Sometimes while overloading a method with varargs parameter, we get ambiguous error. In this guide, you will learn when and why we get this error and how to avoid this error.
Ambiguity in Varargs in Java while method overloading
In the following example, we have are doing method overloading. There are two variants of method disp()
. When we call the method like this: obj.disp(10, 20, 30);
, it gives a compilation error “Reference to method name is ambiguous”.
The reason why we are getting this error is: The compiler is not sure which method to call as both these method can accept this argument list. When we called the method while passing three integer values, compiler doesn’t understand whether all three values are part of varargs parameter (first method) or one belongs to parameter “n” and other two are for varargs (second method).
public class JavaExample { public void disp(int... num){ System.out.println("Displaying Parameter of varargs method:"); for(int temp: num){ System.out.println(temp); } } public void disp(int n, int... num2){ System.out.println("Displaying Parameter of varargs method: "); System.out.println("First Argument: "+ n); System.out.println("Second Argument print: "); for(int temp: num2){ System.out.println(temp); } } public static void main( String[] args ) { JavaExample obj = new JavaExample(); //compiler is confused which method to call //both the variations of disp() method can accept this // argument list obj.disp(10, 20, 30); } }
Output:
As you can see, the program gave compilation error, this is because the compiler is confused which method to call.
Both the methods can accept this method call.
This can be solved by avoiding method overloading and declare one of the method with different name, as shown below:
public class JavaExample { public void disp(int... num){ System.out.println("Displaying Parameter of varargs method:"); for(int temp: num){ System.out.println(temp); } } public void disp2(int n, int... num2){ System.out.println("Displaying Parameter of varargs method: "); System.out.println("First Argument: "+ n); System.out.println("Second Argument print: "); for(int temp: num2){ System.out.println(temp); } } public static void main( String[] args ) { JavaExample obj = new JavaExample(); //This time, it will not throw any error as we have removed //method overloading and renamed the method with different name, //there is only one method with disp() name so no confusion in this case obj.disp(10, 20, 30); } }
Output:
Displaying Parameter of varargs method: 10 20 30
Another example of varargs where ambiguity can occur
In this example, we are not passing any argument while calling the method. This can also result in a compilation error as both the variants of method accept this argument.
public class JavaExample { public void printArgs(int... num){ System.out.println("Printing integers: "); for(int temp: num){ System.out.println(temp); } } public void printArgs(boolean... bool){ System.out.println("Printing boolean values: "); for(boolean b: bool){ System.out.println(b); } } public static void main( String[] args ) { JavaExample obj = new JavaExample(); obj.printArgs(true, false); //no confusion obj.printArgs(10, 20); //no confusion //confusion as no argument is provided, both the variants //of printArgs() method can accept null argument obj.printArgs(); } }
Output: