What does “The constructor … is ambiguous” mean?

后端 未结 4 998
花落未央
花落未央 2021-01-07 17:37

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

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

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 17:53

    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
        }
    }
    

提交回复
热议问题