13

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

3
  • 1
    You need to accept answers to your other questions. Commented Jan 1, 2013 at 18:00
  • @Sean Didn't know that. I'm new here, I'll get to it now. Commented Jan 1, 2013 at 18:03
  • 1
    Because you are wrong. 'super' does refer to an instance. Commented Jan 1, 2013 at 22:21

5 Answers 5

14

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.

Sign up to request clarification or add additional context in comments.

Comments

14

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.

3 Comments

Are you sure? I thought super referred to the Object from which the current Object is extended - which may or may not be instantiated (which is why you can access static variables from the superclass)
Well it seems super does refer to an instance because the JLS says so.
BUT, to answer your other concern, super's referring to an instance does not hamper its ability to access static variables of the superclass (for the same reason instance methods can access static variables of the superclass - Instance variables cannot be referred to from a static context, but static variables CAN be referred to from a non-static context).
10

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();
        . . .
    }
}

Comments

0

Super is a non static variable and non static entity cannot be accessed from static context.

1 Comment

It's not really a viariable, but a reference
0

enter image description here

 super & this are build in non-static reference keywords
 these must be accessed with the non-static methods of the childClass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.