Till now we have learnt than in Java when we invoke a method or constructor then we need to refer its parameter list, the argument that we issue should be same as the parameter list otherwise we will get an error at the time of compilation. Now think about a situation where we need to invoke a constructor or method that has a parameter list that is different from its argument list. As per our earlier learning this may not seem attainable. However Variable Arguments, which is also known as VAR-ARGS, provides us an approach through which we can invoke constructors or methods with parameter lists different from the argument lists.
Syntax: -
<data_type> … <variable_argument_name>.
int … a1, String … s1, float … f1.
• A variable argument internally works on one dimensional or 1-D array concept.
• It stores the argument using index representation.
• It follows added order approach to store the arguments.
• In the case of variable arguments, we can issue many arguments, however the values issued should be compatible with the arguments.
• We can define a variable argument along with different types of parameters, however the variable argument parameter must be the last parameter inside the parameter list.
• In Java we can’t define more than one type of variable argument at a time.
class JTC{
public static void main(String arg []){
A obj = new A();
obj.m1();
obj.m1(10);
obj.m1(10,20);
obj.m1(10,20,30,40,50,60,70,80,90);
String ar1[] = {"Welcome","To","JTC","Tutorial"};
obj.m1(ar1);
obj.m1("Hello",10,20,30,40);
}
}
class A{
void m1(){
System.out.println("m1() in A class");
}
void m1(int a){
System.out.println("m1(int) in A class");
System.out.println("a :- "+a);
}
void m1(int a, int b){
System.out.println("m1(int,int) in A class");
System.out.println("a :- "+a);
System.out.println("b :- "+b);
}
void m1(int ... a){
System.out.println("m1(int ... a) in A class");
for(int i = 0; i <= a.length-1; i++){
System.out.println("Index :- "+i +" Value :- "+a[i]);
}
}
void m1(String a[]){
System.out.println("m1(int a[]) in A class");
for(int i = 0; i <= a.length-1; i++){
System.out.println("Index :- "+i +" Value :- "+a[i]);
}
}
void m1(String s,int ... a){
System.out.println("m1(String s, int ... a) in A class");
System.out.println("s :- "+s);
for(int i = 0; i <= a.length-1; i++){
System.out.println("Index :- "+i +" Value :- "+a[i]);
}
}
/*void m1(int ... a, String s){
// Not Allowed
}*/
}
m1() in A class m1(int) in A class a :- 10 m1(int,int) in A class a :- 10 b :- 20 m1(int ... a) in A class Index :- 0 Value :- 10 Index :- 1 Value :- 20 Index :- 2 Value :- 30 Index :- 3 Value :- 40 Index :- 4 Value :- 50 Index :- 5 Value :- 60 Index :- 6 Value :- 70 Index :- 7 Value :- 80 Index :- 8 Value :- 90 m1(int a[]) in A class Index :- 0 Value :- Welcome Index :- 1 Value :- To Index :- 2 Value :- JTC Index :- 3 Value :- Tutorial m1(String s, int ... a) in A class s :- Hello Index :- 0 Value :- 10 Index :- 1 Value :- 20 Index :- 2 Value :- 30 Index :- 3 Value :- 40
In the above example we have a class which is A and inside the A class we have loaded m1 method with no parameter m1(), int type of parameter m1(int), two int type of parameter m1(int, int), int type of variable argument m1(int … a), a string [] parameter m1(String a[]) and string and int type of variable argument m1(String s, int … a) and one more method which is m1(int … a, String s) - but this method will not be valid because int …a is not the last parameter of m1().