...
| Code Block | ||
|---|---|---|
| ||
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");
}
public static void main(String[] args) {
displayBooleans(true, false);
}
}
|
When run, this program outputsoutput:
| Code Block |
|---|
Overloaded method invoked |
because the non-variable nonvariable arity definition is more specific and consequently a better fit for the arguments given. However, this complexity is best avoided.
...
Bibliography
Item 42: , "Use Varargs Judiciously" | |
"Using the Varargs Language Feature" | |
[Sun 2006] |
...