Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this and super in java

Tags:

java

this and super are keywords aren't they; then how can I use them for passing arguments to constructors the same way as with a method?? In short how is it that both can show such distinct behaviors??

like image 927
abson Avatar asked May 21 '10 15:05

abson


People also ask

What is this () and super () in Java?

Difference between super() and this() in java Programthis keyword mainly represents the current instance of a class. On other hand super keyword represents the current instance of a parent class. 2. Interaction with class constructor. this keyword used to call default constructor of the same class.

Can we use this () and super () in a method in Java?

No, we cannot use this() and super() in a method in java.

What is difference between this and super constructor in Java?

Difference Between this() and super() ConstructorThe this() constructor refers to the current class object. The super() constructor refers immediate parent class object. It is used for invoking the current class method. It is used for invoking parent class methods.

What is the use of this & Super keyword?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.


1 Answers

You are correct that both this and super are keywords. The Java language specification defines explicitly how they must behave. The short answer is that these keywords behave specially because the specification says that they must.

According to the specification this can be used a primary expression (only in certain places) or in an explicit constructor invocation.

The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.

So you can use this as an argument to a function to pass a reference to the current object. However note that you cannot use super in the same way as it is not a primary expression:

public class Program
{   
    void test(Program p) {}

    void run() { test(super); }

    public static void main(String[] args)
    {
        new Program().run();
    }
}

Result:

Program.java:5: '.' expected
    void run() { test(super); }

You can use super.foo though because this is defined in 15.11 to be valid:

FieldAccess:
    Primary . Identifier
    super . Identifier
    ClassName .super . Identifier

The specification also puts restrictions on how super can be used:

The special forms using the keyword super are valid only in an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class; these are exactly the same situations in which the keyword this may be used (§15.8.3).

like image 92
Mark Byers Avatar answered Oct 12 '22 06:10

Mark Byers