简体   繁体   中英

Why doesn't Netbeans see which of my varargs-method I mean?

The code has a problem in line 18 n.fun1("11",1); in netbeans,but not in other IDE this problem only in NetBeans 7.0.1 the IDE shows:

reference to fun1 is ambiguous, both method fun1(java.lang.String,java.lang.Object...) in Test and method fun1(java.lang.String,int,java.lang.Object...) in Test match

The corresponding code is here:

interface Test {
    public void fun1(String str, Object... objs);
    public void fun1(String str, int i, Object... objs);
}
public class NewClass implements Test {
    public void fun1(String str, Object... objs) {
        System.out.println("111111111111111111111");
    }
    public void fun1(String str, int i, Object... objs) {
        System.out.println("2222222222222");
    }
    public static void main(String[] args) {
        Test n = new NewClass();
        n.fun1("11", 1);
    }
}

There is no such method in your interface fun1("11", 1); // fun1(String, int) fun1("11", 1); // fun1(String, int) , try this:

n.fun1("11", new Integer(1));

I faced the same issue. I found it to be already reported to Netbeans team here: https://netbeans.org/bugzilla/show_bug.cgi?id=200024

Your code should compile with JDK6, but not with JDK7. According to Netbeans folks, the Java spec does not allow these two methods in a single class. In their opinion, the fact that JDK6 does not complain about it should be considered as a bug, that has been fixed in JDK7

That's why they answered they would not remove (or make optional) this feature in Netbeans. Even if it looks a bit inconsistent when using JDK6.

In my case, we renamed one of the two methods, so that to be JDK7-compliant.

Updated

After rereading your question I can see the real problem here. There are two fun1 methods in your interface.

public void fun1(String str, Object... objs) // first

and

public void fun1(String str, int i, Object... objs); // second

The problem is, where you're calling the method of your class:

n.fun1("11", 1);

The IDE could not decide whether you want to call your first method or your second.

Because, it can be the first with autoboxing your 1 parameter to an Integer object. Or it can be the second, with the objs parameter as an empty array.

There is no way to fix this without modifying your interface. My java compiler (1.6.0_26) can compile it without any problem and it calls your second method.

But as the IDE says it's ambiguous and it is right about that, this definition is confusing and should be avoided.

Which JDK is your netbeans using? Could it be an old (really old) one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM