Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

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...

like image 537
PrashanD Avatar asked Jan 01 '13 17:01

PrashanD


People also ask

Why Super Cannot be used in static context?

Where the "super" keyword in Java is used as a reference to the object of the superclass. This implies that to use "super" the method should be invoked by an object, which static methods are not. Therefore, you cannot use the "super" keyword from a static method.

Why non-static variable Cannot be referenced from a static context?

Why does this error occur? For the non-static variable, there is a need for an object instance to call the variables. We can also create multiple objects by assigning different values for that non-static variable. So, different objects may have different values for the same variable.

How do you fix non-static method Cannot be referenced from a static context?

There is one simple way of solving the non-static variable cannot be referenced from a static context error. In the above code, we have to address the non-static variable with the object name. In a simple way, we have to create an object of the class to refer to a non-static variable from a static context.

Can you access a non-static variable in the static context?

“Can a non-static method access a static variable or call a static method” is one of the frequently asked questions on static modifier in Java, the answer is, Yes, a non-static method can access a static variable or call a static method in Java.


3 Answers

Here is the section in the JLS about the super keyword:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11.2

The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.

The form T.super.Identifier refers to the field named Identifier of the lexically enclosing instance corresponding to T, but with that instance viewed as an instance of the superclass of T.

In both cases, it is clear that an instance object is needed.


Also, a static context is somewhat different from an instance context, as a class can't override static methods, only hide them.

like image 84
Sean Patrick Floyd Avatar answered Sep 21 '22 00:09

Sean Patrick Floyd


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.

like image 37
Sean Owen Avatar answered Sep 19 '22 00:09

Sean Owen


You can't use super from a static context for the same reason you can't use this in a static context. In both cases, the word refers to an instance.

In a static context, you can always use the name of the superclass explicitly:

class Sub extends Base {
    static void func() {
        Base.func();
        . . .
    }
}
like image 27
Ted Hopp Avatar answered Sep 23 '22 00:09

Ted Hopp