To resolve an unresolved symbolic reference from D to a method in a class C, the symbolic reference to C given by the method reference is first resolved (§5.4.3.1). Therefore, any exception that can be thrown as a result of failure of resolution of a class reference can be thrown as a result of failure of method resolution. If the reference to C can be successfully resolved, exceptions relating to the resolution of the method reference itself can be thrown.
When resolving a method reference:
-
If C is an interface, method resolution throws an
IncompatibleClassChangeError. -
Otherwise, method resolution attempts to locate the referenced method in C and its superclasses:
-
If C declares exactly one method with the name specified by the method reference, and the declaration is a signature polymorphic method (§2.9), then method lookup succeeds. All the class names mentioned in the descriptor are resolved (§5.4.3.1).
The resolved method is the signature polymorphic method declaration. It is not necessary for C to declare a method with the descriptor specified by the method reference.
-
Otherwise, if C declares a method with the name and descriptor specified by the method reference, method lookup succeeds.
-
Otherwise, if C has a superclass, step 2 of method resolution is recursively invoked on the direct superclass of C.
-
-
Otherwise, method resolution attempts to locate the referenced method in the superinterfaces of the specified class C:
- If the maximally-specific superinterface methods of C for the name and descriptor specified by the method reference include exactly one method that does not have its
ACC_ABSTRACTflag set, then this method is chosen and method lookup succeeds. - Otherwise, if any superinterface of C declares a method with the name and descriptor specified by the method reference that has neither its
ACC_PRIVATEflag nor itsACC_STATICflag set, one of these is arbitrarily chosen and method lookup succeeds. - Otherwise, method lookup fails.
- If the maximally-specific superinterface methods of C for the name and descriptor specified by the method reference include exactly one method that does not have its
A maximally-specific superinterface method of a class or interface C for a particular method name and descriptor is any method for which all of the following are true:
- The method is declared in a superinterface (direct or indirect) of C.
- The method is declared with the specified name and descriptor.
- The method has neither its
ACC_PRIVATEflag nor itsACC_STATICflag set. - Where the method is declared in interface I, there exists no other maximally-specific superinterface method of C with the specified name and descriptor that is declared in a subinterface of I.
The result of method resolution is determined by whether method lookup succeeds or fails:
-
If method lookup fails, method resolution throws a
NoSuchMethodError. -
Otherwise, if method lookup succeeds and the referenced method is not accessible (§5.4.4) to D, method resolution throws an
IllegalAccessError. -
Otherwise, let
<E,L_1>be the class or interface in which the referenced methodmis actually declared, and letL_2be the defining loader of D.Given that the return type of
mis T_r, and that the formal parameter types ofmare T_{f1}, ..., T_{fn}, then:If T_r is not an array type, let T_0 be T_r; otherwise, let T_0 be the element type (§2.4) of T_r.
For i = 1 to n: If T_{fi} is not an array type, let T_i be T_{fi}; otherwise, let T_i be the element type (§2.4) of T_{fi}.
The Java Virtual Machine must impose the loading constraints T_i^{L_1} = T_i^{L_2} for i = 0 to n (§5.3.4).
When resolution searches for a method in the class's superinterfaces, the best outcome is to identify a maximally-specific non-abstract method. It is possible that this method will be chosen by method selection, so it is desirable to add class loader constraints for it.
Otherwise, the result is nondeterministic. This is not new: The Java® Virtual Machine Specification has never identified exactly which method is chosen, and how "ties" should be broken. Prior to Java SE 8, this was mostly an unobservable distinction. However, beginning with Java SE 8, the set of interface methods is more heterogenous, so care must be taken to avoid problems with nondeterministic behavior. Thus:
- Superinterface methods that are
privateandstaticare ignored by resolution. This is consistent with the Java programming language, where such interface methods are not inherited. - Any behavior controlled by the resolved method should not depend on whether the method is
abstractor not.
Note that if the result of resolution is an abstract method, the referenced class C may be non-abstract. Requiring C to be abstract would conflict with the nondeterministic choice of superinterface methods. Instead, resolution assumes that the run time class of the invoked object has a concrete implementation of the method.