---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 10 Edition"
java_version: 10
chapter: "Chapter 15. Expressions"
section: "15.12.3"
heading: "15.12.3. Compile-Time Step 3: Is the Chosen Method Appropriate?"
parent: "15.12. Method Invocation Expressions"
source: https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.12.3
id: "15.12.3"
url: https://jvm.obfuscated.software/jls/se10/15.12.3.html
raw: https://jvm.obfuscated.software/jls/se10/15.12.3.md
---

# 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.

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](6.6.md)) 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](8.1.3.md)).
  - Otherwise, let C be the immediately enclosing class of which the compile-time declaration is a member. If the method invocation is not directly enclosed by C or an inner class of C, then a compile-time error occurs.

- If the form is *TypeName* `.` *[TypeArguments]* *Identifier*, then the compile-time declaration must be `static`, 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 a `static` method 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.

- 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.

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

  - If *TypeName* denotes an interface, let T be the type 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](9.4.1.md)) the compile-time declaration from a direct superclass or direct superinterface of T.

    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 `super` method 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](15.12.2.6.md)).

- 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.7](3.10.7.md)) is treated as having the type `Void`.

  - The compile-time result is determined as follows:

    - If the signature polymorphic method is either `void` or has a return type other than `Object`, the compile-time result is the result of the invocation type of the compile-time declaration ([§15.12.2.6](15.12.2.6.md)).
    - 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](15.16.md)), the compile-time result is the erasure of the type of the cast expression ([§4.6](4.6.md)).
    - Otherwise, the compile-time result is the signature polymorphic method's return type, `Object`.

A method is *signature polymorphic* if all of the following are true:

- It is declared in the `java.lang.invoke.MethodHandle` class or the `java.lang.invoke.VarHandle` class.
- It has a single variable arity parameter ([§8.4.1](8.4.1.md)) 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 type of the method invocation ([§13.1](13.1.md)).

- 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 `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 *TypeName* `.` `super` `.` *Identifier*, then the invocation mode is `super`.
  - Otherwise, if the qualifying type of the method invocation is an interface, then the invocation mode is `interface`.
  - Otherwise, the invocation mode is `virtual`.

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](5.1.10.md)) to the return type of the invocation type of the compile-time declaration.
