6

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

Dan
  • 7,286
  • 6
  • 49
  • 114
abson
  • 9,148
  • 17
  • 50
  • 69

7 Answers7

8

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

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

The Java language provides specific handling for these two keywords and they are allowed in limited contexts.

Invoking this(...) will result in bytecode that will invoke the corresponding constructor on the current class, while invoking super(...) will result in bytecode that will invoke the corresponding constructor on the supertype.

Java provides special handling for these because their binding is different from that of normal methods (i.e., you want to avoid dynamic invocation, or you would never manage to get the constructor on the supertype).

Every language has to deal with this problem. In C++, for example, you explicitly specify the name of the parent method instead of using super.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • is there any coding under super and this for this type of behaviour? – abson May 21 '10 at 16:04
  • 1
    @abson It is coded but not by you. It's a part of the JVM I suppose, or maybe the compiler. You should regard this and super the same as + and -, the compiler just knows what to with them. – extraneon May 21 '10 at 16:12
1

I'm not entirely sure what you're asking here, but this is the way the language is designed. The compiler knows that when you do this in a constructor:

super("I'm a string!", 32);

it should find a constructor in the superclass that takes a String and an int as parameters.

Syntactic
  • 10,721
  • 3
  • 25
  • 25
1

From your sub-class you can pass the variables provided to your parent. Examples are better then long explanations so here's a pretty generic example of Extending the Exception Class for your own usage:

public class MyException extends Exception {    
public MyException()
{

}

public MyException(String message)
{
    super(message);
}

public MyException(String string, Throwable e) 
{
    super(string, e);
} 
} 
lees2bytes
  • 296
  • 2
  • 9
0

how is it that these can show such distinct behaviours

The question doesn't make sense. All keywords have distinct behaviours. That's what they're for.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

I am not sure what is the doubt here. This this refers to the reference to the current instance, also when called like this(arg) it calls the corresponding constructor in the current class. Similarly, when super() is called it calls the corresponding constructor in the super class. They can be called only from a constructor.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
-1

According to Wikipedia, this and super are keywords, which is how they get away with all their magic, I suppose.

Edward Dale
  • 29,597
  • 13
  • 90
  • 129
  • According to the [JLS](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9) they are keywords. Wikipedia is not a normative reference. – user207421 Jun 28 '15 at 10:16