The JVM identifies methods that can take different number of parameters using the three dots "..." in the method declaration and handles different number of arguments correspondingly. This asset makes use of the JDK1.5 Enhanced for loop as well. Pls check my other mighty asset on using the enhanced for loop.
package test; public class UnknownNumOfParamsForMethodTest { public static int sum(int...numbers) { int sum=0; for (int d : numbers ) sum =sum + d; return sum; } public static void main(String[] args) { int i=3; int j=6; int k=9; System.out.println("Sum of two numbers "+ sum(i,j)); System.out.println("Sum of three number "+ sum(i,j,k)); } }