Rules for Variable Argument List (var-args)

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){}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: