If there is a most specific method declaration for a method invocation, it is called the compile-time declaration for the method invocation.
It is a compile-time error if an argument to a method invocation is not compatible with its target type, as derived from the invocation type of the compile-time declaration.
If the compile-time declaration is applicable by variable arity invocation, then where the last formal parameter type of the invocation type of the method is F_n[], it is a compile-time error if the type which is the erasure of F_n is not accessible (§6.6) at the point of invocation.
If the compile-time declaration is void, then the method invocation must be a top level expression (that is, the Expression in an expression statement or in the ForInit or ForUpdate part of a for statement), or a compile-time error occurs. Such a method invocation produces no value and so must be used only in a situation where a value is not needed.
In addition, whether the compile-time declaration is appropriate may depend on the form of the method invocation expression before the left parenthesis, as follows:
-
If the form is MethodName - that is, just an Identifier - and the compile-time declaration is an instance method, then:
- It is a compile-time error if the method invocation occurs in a static context (§8.1.3).
- Otherwise, let T be the class or interface to search (§15.12.1). It is a compile-time error if either the method invocation occurs in an early construction context (§8.8.7) of class T, or the innermost enclosing class or interface declaration of the method invocation is neither T nor an inner class of T.
-
If the form is TypeName
.[TypeArguments] Identifier, then the compile-time declaration must bestatic, or a compile-time error occurs. -
If the form is ExpressionName
.[TypeArguments] Identifier or Primary.[TypeArguments] Identifier, then the compile-time declaration must not be astaticmethod declared in an interface, or a compile-time error occurs. -
If the form is
super.[TypeArguments] Identifier, then:- It is a compile-time error if the compile-time declaration is
abstract. - It is a compile-time error if the method invocation occurs in a static context.
- It is a compile-time error if the method invocation occurs in an early construction context of the current class.
- It is a compile-time error if the compile-time declaration is
-
If the form is TypeName
.super.[TypeArguments] Identifier, then:-
It is a compile-time error if the compile-time declaration is
abstract. -
It is a compile-time error if the method invocation occurs in a static context.
-
It is a compile-time error if the method invocation occurs in an early construction context of the class named by TypeName.
-
If TypeName denotes a class C, then if the class or interface declaration immediately enclosing the method invocation is not C or an inner class of C, a compile-time error occurs.
-
If TypeName denotes an interface, let E be the class or interface declaration immediately enclosing the method invocation. A compile-time error occurs if there exists a method, distinct from the compile-time declaration, that overrides (§9.4.1) the compile-time declaration from a direct superclass or direct superinterface of E.
In the case that a superinterface overrides a method declared in a grandparent interface, this rule prevents the child interface from "skipping" the override by simply adding the grandparent to its list of direct superinterfaces. The appropriate way to access functionality of a grandparent is through the direct superinterface, and only if that interface chooses to expose the desired behavior. (Alternately, the programmer is free to define an additional superinterface that exposes the desired behavior with a
supermethod invocation.)
-
The compile-time parameter types and compile-time result are determined as follows:
-
If the compile-time declaration for the method invocation is not a signature polymorphic method, then:
- The compile-time parameter types are the types of the formal parameters of the compile-time declaration.
- The compile-time result is the result of the invocation type of the compile-time declaration (§15.12.2.6).
-
If the compile-time declaration for the method invocation is a signature polymorphic method, then:
-
The compile-time parameter types are the types of the actual argument expressions. An argument expression which is the null literal
null(§3.10.8) is treated as having the typeVoid. -
The compile-time result is determined as follows:
- If the signature polymorphic method is either
voidor has a return type other thanObject, the compile-time result is the result of the invocation type of the compile-time declaration (§15.12.2.6). - Otherwise, if the method invocation expression is an expression statement, the compile-time result is
void. - Otherwise, if the method invocation expression is the operand of a cast expression (§15.16), the compile-time result is the erasure of the type of the cast expression (§4.6).
- Otherwise, the compile-time result is the signature polymorphic method's return type,
Object.
- If the signature polymorphic method is either
-
A method is signature polymorphic if all of the following are true:
- It is declared in the
java.lang.invoke.MethodHandleclass or thejava.lang.invoke.VarHandleclass. - It has a single variable arity parameter (§8.4.1) whose declared type is
Object[]. - It is
native.
The following compile-time information is then associated with the method invocation for use at run time:
-
The name of the method.
-
The qualifying class or interface of the method invocation (§13.1).
-
The number of parameters and the compile-time parameter types, in order.
-
The compile-time result.
-
The invocation mode, computed as follows:
- If the compile-time declaration has the
staticmodifier, then the invocation mode isstatic. - Otherwise, if the part of the method invocation before the left parenthesis is of the form
super.Identifier or of the form TypeName.super.Identifier, then the invocation mode issuper. - Otherwise, if the qualifying class or interface of the method invocation is in fact an interface, then the invocation mode is
interface. - Otherwise, the invocation mode is
virtual.
- If the compile-time declaration has the
If the result of the invocation type of the compile-time declaration is not void, then the type of the method invocation expression is obtained by applying capture conversion (§5.1.10) to the return type of the invocation type of the compile-time declaration.