11. Variable Arity Methods

11.1 Varargs Method: Inroduction

Fixed Arity Methods: It defines fixed number of parameters in its formal parameter declaration. To call the method, correct number of parameters should be passed i.e. if method declaration has 3 formal parameters then the method call should supply three parameters.

Let's build a class VarArgsMehtod to learn about the fixed arity and variable arity / varargs method in Java.

			package javatutorial;

			/**
			 * Example class learn about fixed arity and 
			 * variable arity / varargs method in Java
			 * 
			 * @author f5java.com
			 */
			public class VarArgsMehtod {
				
				/**
				 * Prints name,age and salary details 
				 */
				// 1. Fixed arity method
				public void fixedArityMethod (int age, float salary, String name){
					System.out.println("Inside fixedArity method");
					System.out.println("Name : " + name);
					System.out.println("Age : " + age);
					System.out.println("Salary : " + salary);
				}
				
				public static void main(String[] args) {
					// Instantiate and call the method
					VarArgsMehtod varargs = new VarArgsMehtod();
					
					System.out.println("Calling fixed arity method");
					// 2. Fixed arity method: the number of formal parameter and actual parameter should match
					// i.e. correct parameters has to be passed in method call as per method declaration
					varargs.fixedArityMethod(18, 5000.00f, "John");
				
				}
			}
		

Variable Arity Methods: In variable arity methods the number of arguments in the call can be varied. The syntax of the last formal parameter in variable arity method declaration is as follows

<type>... <formal parameter name>

Let’s look at each element in the variable arity / varargs method's syntax.

Following Rules are to be complied with while using variable arity methods:

Let's add a varargs method to class VarArgsMehtod from the previous chapter.

			package javatutorial;

			/**
			 * Example class learn about fixed arity and 
			 * variable arity / varargs method in Java
			 * 
			 * @author f5java.com
			 */
			public class VarArgsMehtod {
				
				/**
				 * Prints name,age and salary details 
				 */
				// 1. Fixed arity method
				public void fixedArityMethod (int age, float salary, String name){
					System.out.println("Inside fixedArity method");
					System.out.println("Name : " + name);
					System.out.println("Age : " + age);
					System.out.println("Salary : " + salary);
				}
				
				// 3. Variable arity method : Note the following things 
				
				// 3a. ... notation indicates this is a varargs parameter
				// 3b. Only one varargs parameter in formal parameter list
				// 3c. Varargs parameter should always be the last parameter
				// 3d. String... address is treated as String[] address
				
				public void varibleArityMethod(String name, String... address){ // String[] address
					System.out.println("Inside Variable arity method");
					System.out.println(name + " Lenght of varargs parameter is : " + address.length);
				}
				
				public static void main(String[] args) {
					// Instantiate and call the method
					VarArgsMehtod varargs = new VarArgsMehtod();
					
					System.out.println("Calling fixed arity method");
					// 2. Fixed arity method: the number of formal parameter and actual parameter should match
					// i.e. correct parameters has to be passed in method call as per method declaration
					varargs.fixedArityMethod(18, 5000.00f, "John");
				
				}
			}