Java calling method causes ambiguity: The method XXX is ambiguous for the type XX

Source: Internet
Author: User

The method XXX is ambiguous for the type XX

Eclipse compilation error.

Analysis Reason: Method ambiguity, which is not determined by the compiler, which method is used in the code.

1, the call is the method of their own writing

public class Ambiguous {
public static void Main (string[] args) {
Ambiguous ambiguous = new ambiguous ();
Ambiguous. Para (NULL);
}

public void Para (String a) {
System.out.println ("String Type a" + a);
}

public void Para (StringBuffer a) {
System.out.println ("StringBuffer" + a.tostring ());
}
}

Obviously null as a parameter causes the compiler to not recognize which method to call.

In this case, one exception is that if the parameter of the method has an inheritance relationship, then the compiler can determine which method to call, and the principle is "I want you to have it."

2, call the library method.

Some library static methods are many static fields, which are often written to facilitate invocation calls:

Import static org.csdn.linghushaoxia.demo.*;
<pre name= "code" class= "java" >import static org.csdn.linghushaoxia.demo1.*;


If, incidentally, in other packages, there is the same method, can be troublesome, not easy to find.

For example, the Demo.init () static method is called, and the code writes init () directly;

There are Demo1.init () in the Demo1, and the code is written directly to Init (), which inevitably causes ambiguity.

After finding out the ambiguity, it is good to solve, plus the package on it.

This way:

Org.csdn.linghushaoxia.Demo.init ();


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java calling method causes ambiguity: The method XXX is ambiguous for the type XX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.