JVM Knowledge

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error. In cases such as an explicitly typed lambda expression argument (§15.27.1) or a variable arity invocation (§15.12.2.4), some flexibility is allowed to adapt one signature to the other.

One applicable method m_1 is more specific than another applicable method m_2, for an invocation with argument expressions e_1, ..., e_k, if any of the following are true:

  • m_2 is generic, and m_1 is inferred to be more specific than m_2 for argument expressions e_1, ..., e_k by §18.5.4.
  • m_2 is not generic, and m_1 and m_2 are applicable by strict or loose invocation, and where m_1 has formal parameter types S_1, ..., S_n and m_2 has formal parameter types T_1, ..., T_n, the type S_i is more specific than T_i for argument e_i for all i (1 ≤ i ≤ n, n = k).
  • m_2 is not generic, and m_1 and m_2 are applicable by variable arity invocation, and where the first k variable arity parameter types of m_1 are S_1, ..., S_k and the first k variable arity parameter types of m_2 are T_1, ..., T_k, the type S_i is more specific than T_i for argument e_i for all i (1 ≤ i ≤ k). Additionally, if m_2 has k+1 parameters, then the k+1'th variable arity parameter type of m_1 is a subtype of the k+1'th variable arity parameter type of m_2.

The above conditions are the only circumstances under which one method may be more specific than another.

A type S is more specific than a type T for any expression if S <: T (§4.10).

A functional interface type S is more specific than a functional interface type T for an expression e if T is not a subtype of S and one of the following is true (where U_1 ... U_k and R_1 are the parameter types and return type of the function type of the capture of S, and V_1 ... V_k and R_2 are the parameter types and return type of the function type of T):

  • If e is an explicitly typed lambda expression (§15.27.1), then one of the following is true:

    • R_2 is void.

    • R_1 <: R_2.

    • R_1 and R_2 are functional interface types, and there is at least one result expression, and R_1 is more specific than R_2 for each result expression of e.

      (The result expression of a lambda expression with a block body is defined in §15.27.2; the result expression of a lambda expression with an expression body is simply the body itself.)

    • R_1 is a primitive type, and R_2 is a reference type, and there is at least one result expression, and each result expression of e is a standalone expression (§15.2) of a primitive type.

    • R_1 is a reference type, and R_2 is a primitive type, and there is at least one result expression, and each result expression of e is either a standalone expression of a reference type or a poly expression.

  • If e is an exact method reference expression (§15.13.1), then i) for all i (1 ≤ i ≤ k), U_i is the same as V_i, and ii) one of the following is true:

    • R_2 is void.
    • R_1 <: R_2.
    • R_1 is a primitive type, R_2 is a reference type, and the compile-time declaration for the method reference has a return type which is a primitive type.
    • R_1 is a reference type, R_2 is a primitive type, and the compile-time declaration for the method reference has a return type which is a reference type.
  • If e is a parenthesized expression, then one of these conditions applies recursively to the contained expression.

  • If e is a conditional expression, then for each of the second and third operands, one of these conditions applies recursively.

A method m_1 is strictly more specific than another method m_2 if and only if m_1 is more specific than m_2 and m_2 is not more specific than m_1.

A method is said to be maximally specific for a method invocation if it is accessible and applicable and there is no other method that is applicable and accessible that is strictly more specific.

If there is exactly one maximally specific method, then that method is in fact the most specific method; it is necessarily more specific than any other accessible method that is applicable. It is then subjected to some further compile-time checks as specified in §15.12.3.

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case:

  • If all the maximally specific methods have override-equivalent signatures (§8.4.2), then:

    • If exactly one of the maximally specific methods is concrete (that is, non-abstract or default), it is the most specific method.

    • Otherwise, if all the maximally specific methods are abstract or default, and the signatures of all of the maximally specific methods have the same erasure (§4.6), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type.

      In this case, the most specific method is considered to be abstract. Also, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the throws clauses of each of the maximally specific methods.

  • Otherwise, the method invocation is ambiguous, and a compile-time error occurs.