BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Ambiguity error while overloading Method with Varargs parameter

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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:
Varargs ambiguity error in Java
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:
Varargs method overloading error while passing null

Recommended Posts

  • Method Overloading in Java
  • Method Overloading – Passing Null Values
  • Java Varargs explained with examples
  • Method Overloading vs Method Overriding
  • Java 9 – @SafeVarargs Annotation (with examples)
❮ Java Programming

Top Related Articles:

  1. Method Overloading “Reference is Ambiguous” error in Java
  2. Java Varargs explained with examples
  3. How to write to file in Java using BufferedWriter
  4. Java StringBuffer setLength() Method
  5. Method Overloading with Autoboxing and Widening in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap