Rules for Variable Argument List (var-args)
July 24, 2009 Leave a comment
As Java alows to create methods that can take variable no of arguments, we do have some rules to be followed such as:
- Var-arg Type: While declaring var-arg parameters, we shoud specify the argument type which can be either primitive type or object type.
void example1(int… x){}
void example2(ClassA… obj){}
- Basic Syntax: For the declaration of var-arg parameter, we will provide ellipsis(…) followed by type then a space and then a name of thee array that will holds the received parameter.
void example(int… x){}
- Other Parameter : We can even provide other parameters in a method that is not var-arg.
void example(int x, char y,char… z){}
- Var-arg limits: The var-arg mustbe the last parameter in the method’s signature, and we can have only one var-arg in a method.
void example(int x, char y,char… z){}

Recent Comments