If you are reading this, chances are you got The method X is ambiguous for the type Y error when compiling a java program in terminal or in any Java IDE.
如果您正在阅读本文,则很可能在终端或任何Java IDE中编译Java程序时The method X is ambiguous for the type Y错误The method X is ambiguous for the type Y 。
Java模棱两可的方法调用 (Java ambiguous method call)
Here I am going to explain why java ambiguous method call error comes with some examples. This ambiguous method call error always comes with method overloading where compiler fails to find out which of the overloaded method should be used.
在这里,我将解释为什么Java歧义方法调用错误带有一些示例。 这种模棱两可的方法调用错误总是伴随着方法重载,而编译器无法找出应该使用哪个重载方法。
Suppose we have a java program like below.
假设我们有一个如下的Java程序。
package com.journaldev.errors;
public class Test {
public void foo(Object o) {
System.out.println("Object");
}
public void foo(String s) {
System.out.println("String");
}
public static void main(String[] args) {
new Test().foo(null);
}
}Above program compiles perfectly and when we run it, it prints “String”.
上面的程序可以完美地编译,当我们运行它时,它会显示“ String”。
So the method foo(String s) was called by the program. The reason behind this is java compiler tries to find out the method with most specific input parameters to invoke a method. We know that Object is the parent class of String, so the choice was easy. Here is the excerpt from Java Language Specification.
因此,程序调用了foo(String s)方法。 这背后的原因是Java编译器试图找出具有最特定输入参数的方法来调用方法。 我们知道Object是String的父类,因此选择很容易。 这是Java语言规范的摘录。
The reason I am passing “null” is because it works for any type of arguments, if we pass any other objects the choice of method for the java compiler is easy.
我传递“ null”的原因是因为它适用于任何类型的参数,如果我们传递任何其他对象,则为Java编译器选择方法很容易。
方法X对于类型Y是不明确的 (The method X is ambiguous for the type Y)
Now let’s add below method to the above code.
现在,将以下方法添加到上面的代码中。
public void foo(Integer i){
System.out.println("Integer");
}You will get compile time error as
The method foo(Object) is ambiguous for the type Test because both String and Integer class have Object as parent class and there is no inheritance. So java compiler doesn’t consider any of them to be more specific, hence the method ambiguous call error.
您将获得编译时错误,因为The method foo(Object) is ambiguous for the type Test因为String和Integer类都将Object作为父类,并且没有继承。 因此,java编译器不认为它们中的任何一个更加具体,因此该方法模棱两可的调用错误。
package com.journaldev.strings;
public class Test {
public void foo(Object o) {
System.out.println("Object");
}
public void foo(Exception e) {
System.out.println("Exception");
}
public void foo(NullPointerException ne) {
System.out.println("NullPointerException");
}
public static void main(String[] args) {
new Test().foo(null);
}
}As above explained, here foo(NullPointerException ne) is the most specific method because it’s inherited from Exception class and hence this code compiles fine and when executed prints “NullPointerException”.
如上所述,此处的foo(NullPointerException ne)是最具体的方法,因为它是从Exception类继承的,因此此代码可以正常编译,并且在执行时显示“ NullPointerException”。
I hope this article clarifies any doubt you have with java ambiguous method call compiler error, please comment if you want to add something or if you have some confusion with this.
我希望本文能澄清您对java歧义方法调用编译器错误的任何疑问,如果要添加某些内容或对此有疑问,请发表评论。
