Java Reference
In-Depth Information
9.6.1. Finding the Right Method
For an invocation of a method to be correct, you must provide argu-
ments of the proper number and type so that exactly one matching
method can be found in the class. If a method is not overloaded, de-
termining the correct method is simple, because only one parameter
count is associated with the method name. When overloaded methods
are involved choosing the correct method is more complex. The compiler
uses a "most specific" algorithm to do the match, the general form of
which is as follows:
1. Determine which class or interface to search for the method. Ex-
actly how this is done depends on the form of the method invoc-
ation. For example, if the invocation is of a static method using
a class namesuch as Math.exp then the class to search for exp is
Math . On the other hand, if the method name is not qualified in
any waysuch as exp(n) then there must be a method by that name
in scope 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 de-
termine where to search.
2. 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.
1. 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
 
 
Search WWH ::




Custom Search