aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference to method is ambiguous Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference to method is ambiguous" Watch "reference to method is ambiguous" New topic
Author

reference to method is ambiguous

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Hello,

I've found some discussions of this error message, but none that exactly reflect my situation.

I have two methods in the same class Utils with the same name among() but different argument classes. Both use variable-length argument lists. (Method contents are irrelevant.)



When I try to compile the entire code base, which contains many calls to among() with arguments of ints, doubles, bytes, longs, and chars, I get many examples of the following error:



If I omit among(int srch, int... arr), on the other hand, everything compiles just fine.

Utils contains many examples of other methods that differ only in their signature, e.g.:





These methods compile just fine. Furthermore, both of the following pairs of methods compile just fine:





So, there appears to be something "special" about the original case that causes the error. Any insights as to what causes the error?

Also, suppose I include only the method among(double, double...); will I be in danger of introducing precision errors if Java casts ints, longs, chars, or bytes to doubles and then compares the doubles to one another with the == operator?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18838
    
  40

Bob Grossman wrote:Hello,

I've found some discussions of this error message, but none that exactly reflect my situation.

I have two methods in the same class Utils with the same name among() but different argument classes. Both use variable-length argument lists. (Method contents are irrelevant.)



When I try to compile the entire code base, which contains many calls to among() with arguments of ints, doubles, bytes, longs, and chars, I get many examples of the following error:



If I omit among(int srch, int... arr), on the other hand, everything compiles just fine.

Utils contains many examples of other methods that differ only in their signature, e.g.:





These methods compile just fine. Furthermore, both of the following pairs of methods compile just fine:





So, there appears to be something "special" about the original case that causes the error. Any insights as to what causes the error?

Also, suppose I include only the method among(double, double...); will I be in danger of introducing precision errors if Java casts ints, longs, chars, or bytes to doubles and then compares the doubles to one another with the == operator?



When it comes to ambiguous methods, it actually depends on how the methods are used. So, the "special" thing that is different is the rest of the code -- something that is using one of the methods, and the compiler can't figure out which one to use.

How does the compiler figure it out? The easiest case is an exact match -- if there is an exact match, then that's the one that is chosen. If there isn't an exact match, then the "most specific" one will be chosen. There is also a third case, related to backward compatibility, and that's done by trying it out without certain features first. In this example, the last feature is for var-args, and both method uses var-args, so the third case don't apply... so, how about the second case -- choosing the "most specific"?

There is a complex mechanism for determining "most specific", but the easiest way to envision it is -- can one method completely handle the parameters of the other method? If so, then the other method is more specific. In this example, however ...



are var-args, so it is really this...



and since it is not possible to implicitly cast from an int array to an double array, and vice versa, then neither method is "more specific" than the other -- and if somewhere in the code, something uses the among() method, with parameters that can match both signatures (but not exactly), then the method call is ambiguous.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Thanks very much for your response.

How does the compiler figure it out? The easiest case is an exact match -- if there is an exact match, then that's the one that is chosen.


But I had many places where I was calling among() with a series of int parameters, e.g., among(bondOrder, 2, 3) (where bondOrder is an int). According to your explanation, the compiler should have realized that among(int, int...) was an exact match, but the compiler still rejected these calls to among(), saying they were ambiguous.

Perhaps it is the case that a method with var-args is *never* considered an exact match, so it always falls back on the "most specific" method?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference to method is ambiguous