What does it mean ?
As the name, the compiler is confused between two constructors and do not know which one it should be using to instantiate the class.
When does it occur ?
When you have - Overloaded Constructors
- With the same number of parameters
A Scenario
class AmbiguousConstructor
{
public AmbiguousConstructor(String s, int i) { ... }
public AmbiguousConstructor(ArrayList<Integer>, int i) { ... }
...
}
Now when we try to create the object
AmbiguousConstructor ac= new AmbiguousConstructor(null,3);
// Here the compiler says the constructor is ambiguous because null can be passed in the place of both String and ArrayList.
How to avoid ambiguous constructor ?
If you want to call the constructor with String parameter, just cast the null to the required type like
AmbiguousConstructor((String) null, 3);
As the name, the compiler is confused between two constructors and do not know which one it should be using to instantiate the class.
When does it occur ?
When you have - Overloaded Constructors
- With the same number of parameters
A Scenario
class AmbiguousConstructor
{
public AmbiguousConstructor(String s, int i) { ... }
public AmbiguousConstructor(ArrayList<Integer>, int i) { ... }
...
}
Now when we try to create the object
AmbiguousConstructor ac= new AmbiguousConstructor(null,3);
// Here the compiler says the constructor is ambiguous because null can be passed in the place of both String and ArrayList.
How to avoid ambiguous constructor ?
If you want to call the constructor with String parameter, just cast the null to the required type like
AmbiguousConstructor((String) null, 3);