Java Program to demonstrate use of varargs (Variable length arguments) in constructor

Write a Java program to demonstrate use of varargs (Variable Length Arguments) in constructor.



Source Code
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);
	}
}
Output
The sum is 10
The sum is 30
The sum is 60