Java Reference
In-Depth Information
This guarantees that any calls that were valid in the Java programming lan-
guage before Java SE 5.0 are not considered ambiguous as the result of
the introduction of variable arity methods, implicit boxing and/or unboxing.
However, the declaration of a variable arity method (§ 8.4.1 ) can change the
method chosen for a given method method invocation expression, because
a variable arity method is treated as a fixed arity method in the first phase.
For example, declaring m(Object...) in a class which already declares m(Object)
causes m(Object) to no longer be chosen for some invocation expressions (such
as m(null) ), as m(Object[]) is more specific.
2. The second phase (§ 15.12.2.3 ) performs overload resolution while allowing box-
ing and unboxing, but still precludes the use of variable arity method invocation. If
no applicable method is found during this phase then processing continues to the
third phase.
This ensures that a method is never chosen through variable arity method in-
vocation if it is applicable through fixed arity method invocation.
3. The third phase (§ 15.12.2.4 ) allows overloading to be combined with variable arity
methods, boxing, and unboxing.
Deciding whether a method is applicable will, in the case of generic methods (§ 8.4.4 ), re-
quire that type arguments be determined. Type arguments may be passed explicitly or im-
plicitly. If they are passed implicitly, they must be inferred (§ 15.12.2.7 ) from the types of
the argument expressions.
If several applicable methods have been identified during one of the three phases of applic-
ability testing, then the most specific one is chosen, as specified in section § 15.12.2.5 .
Example 15.12.2-1. Method Applicability
Click here to view code image
class Doubler {
static int two() { return two(1); }
private static int two(int i) { return 2*i; }
}
class Test extends Doubler {
static long two(long j) { return j+j; }
public static void main(String[] args) {
System.out.println(two(3));
System.out.println(Doubler.two(3)); // compile-time error
}
}
Search WWH ::




Custom Search