Sometimes you do not know how many arguments a method will receive. Java's varargs syntax lets you accept or more arguments of the same type using ... after the type name.
public static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
You can call this as sum(1, 2), sum(1, 2, 3, 4, 5), or even sum() with no arguments at all. Inside the method, numbers is treated as an int[] array.
There are restrictions. A method can have only varargs parameter, and it must be the last parameter in the list. Writing sum(int... numbers, String label) will not compile.