Var-arg type : Variable Length Argument « Object Oriented « SCJP






Methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.

A var-arg parameter is declared with the syntax type... name.

A var-arg method can have only one var-arg parameter.

In methods with normal parameters and a var-arg, the var-arg must come last.

Legal:

void doStuff(int... x) { }  // expects from 0 to many ints as parameters
void doStuff2(char c, int... x)  { }  // expects first a char, then 0 to many ints
void doStuff3(Animal... animal) { }   // 0 to many Animals


Illegal:

void doStuff4(int x...) { }             // bad syntax
void doStuff5(int... x, char... y) { }  // too many var-args
void doStuff6(String... s, byte b) { }  // var-arg must be last








6.6.Variable Length Argument
6.6.1.Var-arg type
6.6.2.Variable-Length Argument Lists