Write a Java program to demonstrate use of varargs (Variable Length Arguments) in constructor.
public class VarArgsConstDemo {
public VarArgsConstDemo(int... a) {
int sum = 0;
for(int i = 0;i < a.length; i++)
{
sum += a[i];
}
System.out.println("The sum is "+sum);
}
public static void main(String[] args) {
VarArgsConstDemo v1 = new VarArgsConstDemo(10);
VarArgsConstDemo v2 = new VarArgsConstDemo(10,20);
VarArgsConstDemo v3 = new VarArgsConstDemo(10,20,30);
}
}
The sum is 10
The sum is 30
The sum is 60