What does "The constructor ... is ambiguous" mean?

29,750

Solution 1

The problem exists when you try to instantiate a class that could apply to more than one constructor.

For example:

public Example(String name) {
    this.name = name;
}

public Example(SomeOther other) {
    this.other = other;
} 

If you call the constructor with a String object, there's one definite constructor. However, if you instantiate new Example(null) then it could apply to either and is therefore ambiguous.

The same can apply to methods with similar signatures.

Solution 2

To add on to other answers, it can be avoided by casting the argument to what is intended, e.g.:

class Foo {

    public Foo(String bar) {}
    public Foo(Integer bar) {}

    public static void main(String[] args) {
        new Foo((String) null);
    }

}

Solution 3

This means that you have two constructors with the same signature, or that you're trying to create a new instance of Case with parameters that could match more than one constructor.

In your case :

Case(Problem, Solution, double, CaseSource)

Java create methods (constructors) signatures with the parameter types. You can have two methods with the same similar parameter types, and therefore it may be possible to generate ambiguous calls by providing ambiguous arguments that could match multiple method (constructor) signatures.

You may reproduce this error (which is not eclipse's fault) with this code :

class A {
    public A(String a) { }
    public A(Integer a) { }

    static public void main(String...args) {
        new A(null);    // <== constructor is ambiguous
    }
}

Solution 4

In other words, it is unclear which of constructors must be called.

Share:
29,750

Related videos on Youtube

karikari
Author by

karikari

Updated on July 09, 2022

Comments

  • karikari
    karikari almost 2 years

    I would like to know what the error message in Eclipse means:

    The constructor Case(Problem, Solution, double, CaseSource) is ambiguous

    • biziclop
      biziclop about 13 years
      What other constructors do you have in the Case class?
  • Harry Joy
    Harry Joy about 13 years
    How can you have two constructors with same signature. In eclipse it will give error: Duplicate method Method(params) in type Cls
  • GuruKulki
    GuruKulki about 13 years
    you cannot have two constructors(methods) of same signature.
  • Deepak Swami
    Deepak Swami over 11 years
    it is a comment rather then an answer
  • Daniel
    Daniel almost 9 years
    This is an answer, but not a useful one. Perhaps a longer explanation would help.

Related