at the point where the code invokes the method. That method will be defined in a particular class or
interface, and that is the class or interface that will be searched in step 2. You should note that it is
only the name of the method that is used to determine where to search.
Find all the methods in that class or interface that could possibly apply to the invocationnamely, all
accessible methods that have the correct name, can take the number of arguments being passed, and
whose parameters are of types that can be assigned the values of all the arguments. This matching
process occurs in three phases:[3]
[3] These phases were introduced to maintain backwards compatibility with older
versions of the Java programming language.
The match is attempted without performing any boxing conversions, and without considering
the possibility of a variable number of argumentsthat is, the compiler looks for a method
whose declared number of parameters matches the number of arguments, and whose kinds of
parameters (either primitive or reference types) match the corresponding arguments provided.
i.
If no matches have been found, the match is attempted again, but this time boxing
conversions are considered, so primitives can be passed for objects and vice-versa.
ii.
If no matches have been found, the match is attempted again, but this time the possibility of a
variable number of arguments is considered, so now the number of arguments might exceed
the number of declared parameters.
iii.
2.
If any method in the set has parameter types that are all assignable to the corresponding parameters of
any other method in the set, that other method is removed from the set because it is a less specific
method. For example, if the set has a method that takes an Object parameter and another that takes
a String parameter, the Object method is removed because a String can be assigned to an
Object, and therefore the method that takes a String is more specific. If you pass a String
argument you want it handled by the method that specializes in strings, not the general one that works
with any object.
3.
If exactly one method remains, that method is the most specific and will be invoked. If more than one
method remains, and they have different signatures, then the invocation is ambiguous and the
invoking code invalid because there is no most specific method. If all the remaining methods have the
same signature then: if all are abstract then one is chosen arbitrarily; otherwise if only one is not
abstract then it is chosen; otherwise the invocation is again ambiguous and is invalid.
4.
The exact details of the algorithm are quite complex, due mainly to the possibility of generic types or methods
being involvedsee Chapter 11. Interested readers should again consult The Java™ Language Specification,
Third Edition, for those details.
Once a method has been selected, that method determines the expected return type and the possible checked
exceptions of that method invocation. If the expected return type is not acceptable in the code (for example,
the method returns a String but the method call is used as an array subscript) or if the checked exceptions
are not dealt with correctly you will get a compile-time error.
For instance, suppose you had the following type hierarchy: