Why can I not use "super" variable from a static context, even though "super" refers to the parent class and NOT a class instance, unlike "this"?

PrashanD picture PrashanD · Jan 1, 2013 · Viewed 20.1k times · Source

I'm talking java language.

Variable "this", when used inside a class, refers to the current instance of that class, which means you cannot use "this" inside a static method.

But "super", when used inside a class, refers to the superclass of that class, not an instance of the superclass, which should mean that you can use "super" inside a static method. But it turns out you cannot.

A possible explanation would be to say that "super" also refers to an instance of the superclass, but I can't see why it should...

Answer

Sean Owen picture Sean Owen · Jan 1, 2013

No, super does refer to an instance -- the same instance that this refers to -- the current object. It's just a way to reference methods and fields in defined in the superclass that are overridden or hidden in the current class.