Compile-Time Step 3: Is the Chosen Method Appropriate?

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?

If there is a most specific method declaration for a method invocation, it is called the compile-time declaration for the method invocation.

If the method invocation has, before the left parenthesis, a MethodName of the form Identifier, and the compile-time declaration is an instance method, then:

• If the method invocation occurs in a static context (§8.1.3), then a compile-time error occurs.

The reason is that a method invocation of this form cannot be used to invoke an instance method in places where this (§15.8.3) is not defined.

• Otherwise, let C be the innermost enclosing class of which the compile-time declaration is a member.

If the invocation is not directly enclosed by C or an inner class of C , then a compile-time error occurs.

If the method invocation has, before the left parenthesis, a MethodName of the form TypeName . Identifier, or if the method invocation, before the left parenthesis, has the form TypeName . NonWildTypeArguments Identifier, then the compile-time declaration must be static , or a compile-time error occurs.

The reason is that a method invocation of this form does not specify a reference to an object that can serve as this within the instance method.

EXPRESSIONS Compile-Time Step 3: Is the Chosen Method Appropriate? 15.12.3

If the method invocation has, before the left parenthesis, the form super . NonWildTypeArguments opt Identifier, then:

• If the compile-time declaration is abstract , a compile-time error occurs. • If the method invocation occurs in a static context, a compile-time error occurs. If the method invocation has, before the left parenthesis, the form ClassName .

super . NonWildTypeArguments opt Identifier, then: • If the compile-time declaration is abstract , a compile-time error occurs. • If the method invocation occurs in a static context, a compile-time error occurs.

• Otherwise, let C be the class denoted by ClassName. If the invocation is not directly enclosed by C or an inner class of C , then a

compile-time error occurs. 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 (§14.8) or in the ForInit or ForUpdate part of a for statement (§14.14)), or a compile-time error occurs.

The reason is that such a method invocation produces no value and so must be used only in a situation where a value is not needed.

A method is signature polymorphic if and only if all of the following conditions hold:

• It is declared in the java.lang.invoke.MethodHandle class. • It takes a single variable arity parameter (§8.4.1) whose declared type is

Object[] . • It has a return type of Object . • It is native .

In Java SE 7, the only signature polymorphic methods are the invoke and invokeExact methods of the class java.lang.invoke.MethodHandle .

If the compile-time declaration for the method invocation is not a signature polymorphic method, then the types of its parameters are the types of its formal parameters, and the result type is its chosen result type.

Otherwise, if the compile-time declaration for the method invocation is a signature polymorphic method, then:

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate? EXPRESSIONS

• The types of its parameters are the static types of the actual argument expressions. An argument expression which is the null literal null (§3.10.7) is treated as

having the static type Void . • The result type is determined as follows: ◆ If the method invocation expression is an expression statement, the method

is void . ◆ Otherwise, if the method invocation expression is the operand of a cast

expression (§15.16), the return type is the erasure (§4.6) of the type of the cast expression.

◆ Otherwise, the return type is the method's declared return type, Object . The following compile-time information is then associated with the method

invocation for use at run-time: • The name of the method. • The qualifying type of the method invocation (§13.1). • The number of parameters and the types of the parameters, in order. • The result type, or void . • The invocation mode, computed as follows:

◆ If the compile-time declaration has the static modifier, then the invocation mode is static .

◆ Otherwise, if the compile-time declaration has the private modifier, then the invocation mode is nonvirtual .

◆ Otherwise, if the part of the method invocation before the left parenthesis is of the form super . Identifier or of the form ClassName . super . Identifier,

then the invocation mode is super . ◆ Otherwise, if the compile-time declaration is in an interface, then the

invocation mode is interface . ◆ Otherwise, the invocation mode is virtual .

If the compile-time declaration for the method invocation is not void , then the type of the method invocation expression is the result type specified in the compile-time declaration.

EXPRESSIONS Run-time Evaluation of Method Invocation 15.12.4