You are correct that and thisand superare the key words. The Java language specification explicitly defines how they should behave. Short answer: these keywords behave on purpose, because the specification says that they should.
According to the specification this, a primary expression can be used (only in certain places) or in an explicit constructor call .
This keyword can only be used 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 elsewhere, a compile-time error occurs.
Thus, you can use thisas an argument functions to pass a reference to the current object. However, note that you cannot use the supersame as this is not the main 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 it super.foobecause it is defined in 15.11 to be valid:
FieldAccess:
Primary . Identifier
super . Identifier
ClassName .super . Identifier
super:
, super, , ; , this (§15.8.3).