The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to search for definitions of methods of that name.
The name of the method is specified by the MethodName or Identifier which immediately precedes the left parenthesis of the MethodInvocation.
For the class or interface to search, there are six cases to consider, depending on the form that precedes the left parenthesis of the MethodInvocation:
-
If the form is MethodName, that is, just an Identifier, then:
If the Identifier appears in the scope of a method declaration with that name (§6.3, §6.4.1), then:
-
If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. The class or interface to search is T.
This search policy is called the "comb rule". It effectively looks for methods in a nested class's superclass hierarchy before looking for methods in an enclosing class and its superclass hierarchy. See §6.5.7.1 for an example.
-
Otherwise, the method declaration may be in scope due to one or more single-static-import or static-import-on-demand declarations. There is no class or interface to search, as the method to be invoked is determined later (§15.12.2.1).
-
-
If the form is TypeName
.[TypeArguments] Identifier, then the type to search is the type denoted by TypeName. -
If the form is ExpressionName
.[TypeArguments] Identifier, then the class or interface to search is the declared type T of the variable denoted by ExpressionName if T is a class or interface type, or the upper bound of T if T is a type variable. -
If the form is Primary
.[TypeArguments] Identifier, then let T be the type of the Primary expression. The class or interface to search is T if T is a class or interface type, or the upper bound of T if T is a type variable.It is a compile-time error if T is not a reference type.
-
If the form is
super.[TypeArguments] Identifier, then the class to search is the superclass of the class whose declaration contains the method invocation.Let T be the type declaration immediately enclosing the method invocation. It is a compile-time error if T is the class
Objector T is an interface. -
If the form is TypeName
.super.[TypeArguments] Identifier, then:-
It is a compile-time error if TypeName denotes neither a class nor an interface.
-
If TypeName denote a class, C, then the class to search is the superclass of C.
It is a compile-time error if C is not a lexically enclosing type declaration of the current class, or if C is the class
Object.Let T be the type declaration immediately enclosing the method invocation. It is a compile-time error if T is the class
Object. -
Otherwise, TypeName denotes the interface to be searched, I.
Let T be the type declaration immediately enclosing the method invocation. It is a compile-time error if I is not a direct superinterface of T, or if there exists some other direct superclass or direct superinterface of T, J, such that J is a subtype of I.
-
The TypeName . super syntax is overloaded: traditionally, the TypeName refers to a lexically enclosing type declaration which is a class, and the target is the superclass of this class, as if the invocation were an unqualified super in the lexically enclosing type declaration.
class Superclass {
void foo() { System.out.println("Hi"); }
}
class Subclass1 extends Superclass {
void foo() { throw new UnsupportedOperationException(); }
Runnable tweak = new Runnable() {
void run() {
Subclass1.super.foo(); // Gets the 'println' behavior
}
};
}
To support invocation of default methods in superinterfaces, the TypeName may also refer to a direct superinterface of the current class or interface, and the target is that superinterface.
interface Superinterface {
default void foo() { System.out.println("Hi"); }
}
class Subclass2 implements Superinterface {
void foo() { throw new UnsupportedOperationException(); }
void tweak() {
Superinterface.super.foo(); // Gets the 'println' behavior
}
}
No syntax supports a combination of these forms, that is, invoking a superinterface method of a lexically enclosing type declaration which is a class, as if the invocation were of the form InterfaceName . super in the lexically enclosing type declaration.
class Subclass3 implements Superinterface {
void foo() { throw new UnsupportedOperationException(); }
Runnable tweak = new Runnable() {
void run() {
Subclass3.Superinterface.super.foo(); // Illegal
}
};
}
A workaround is to introduce a private method in the lexically enclosing type declaration, that performs the interface super call.