Java Reference
In-Depth Information
Bibliography
[Bloch 2005]
Puzzle 4, “It's Elementary”
[JLS 2013]
§3.10.1, “Integer Literals”
[Seacord 2009]
DCL02-C. Use visually distinct identifiers
[Unicode 2013]
51. Avoid ambiguous overloading of variable arity methods
The variable arity (varargs) featurewasintroducedinJDKv1.5.0tosupportmethodsthat
accept a variable number of arguments.
According to the Java SE 6 Documentation [Oracle 2011b],
AsanAPIdesigner,youshoulduse[variablearitymethods]sparingly,onlywhenthe
benefit is truly compelling. Generally speaking, you should not overload a varargs
method, or it will be difficult for programmers to figure out which overloading gets
called.
Noncompliant Code Example
In this noncompliant code example, overloading variable arity methods makes it unclear
which definition of displayBooleans() is invoked:
Click here to view code image
class Varargs {
private static void displayBooleans(boolean... bool) {
System.out.print("Number of arguments: "
+ bool.length + ", Contents: ");
for (boolean b : bool) {
System.out.print("[" + b + "]");
}
}
private static void displayBooleans(boolean bool1,
boolean bool2) {
System.out.println("Overloaded method invoked");
}
Search WWH ::




Custom Search