JVM Knowledge

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 type 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 type 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 class or interface declaration of which that method is a member, let E be the innermost such class or interface declaration. The type to search is the type of E.this (§15.8.4).

      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 type 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 (possibly raw) type denoted by TypeName.

  • If the form is ExpressionName . [TypeArguments] Identifier, then the type 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 type 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 type to search is the direct superclass type of the class whose declaration contains the method invocation.

    Let E be the class or interface declaration immediately enclosing the method invocation. It is a compile-time error if E is the class Object or 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 type to search is the direct superclass type of C.

      It is a compile-time error if C is not a lexically enclosing class declaration of the method invocation, or if C is the class Object.

      Let E be the class or interface declaration immediately enclosing the method invocation. It is a compile-time error if E is the class Object.

    • Otherwise, TypeName denotes an interface, I.

      Let E be the class or interface declaration immediately enclosing the method invocation. It is a compile-time error if I is not a direct superinterface of E, or if there exists some other direct superclass or direct superinterface of E, J, such that J is a subclass or subinterface of I.

      The type to search is the type of I that is a direct superinterface type of E.

The TypeName . super syntax is overloaded: traditionally, the TypeName refers to a lexically enclosing class declaration, and the target is the superclass of this class, as if the invocation were an unqualified super in the lexically enclosing class declaration.

class Superclass {
    void foo() { System.out.println("Hi"); }
}

class Subclass1 extends Superclass {
    public void foo() { throw new UnsupportedOperationException(); }

    Runnable tweak = new Runnable() {
        public 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 {
    public 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 class declaration, as if the invocation were of the form InterfaceName . super in the lexically enclosing class declaration.

class Subclass3 implements Superinterface {
    public 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 class declaration, that performs the interface super call.