This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading (primitive/Wrapper) var-arg methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading (primitive/Wrapper) var-arg methods" Watch "Overloading (primitive/Wrapper) var-arg methods" New topic
Author

Overloading (primitive/Wrapper) var-arg methods

Christian Kindler
Greenhorn

Joined: Apr 16, 2009
Posts: 18
Hi there,

the following code will not compile:


Test.java:12: reference to go is ambiguous, both method go(int...) in Test and method go(java.lang.Integer...) in Test match
go(7);
^
Test.java:13: reference to go is ambiguous, both method go(int...) in Test and method go(java.lang.Integer...) in Test match
go(new Integer(7));
^
2 errors


Question 1: is there any possibility to get a call to one of the go() methods compiled?
Question 2: if the answer to question 1 is "no", why does the compiler complains about accessing the methods an not about defining the to go() methods?


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Christian,

The only way to get a successful call is to do this:


The reason why the original invocations are ambiguous is because they involve the varargs mechanism, and both methods are possible matches. When an invocation matches more than one method in which the varargs mechanism needs to be used (and no other method in which the varargs mechanism isn't used is a match,) the invocation is considered ambiguous by the compiler.

And to clarify: If you pass an array which can directly be matched to the varargs array, you are not involving the varargs mechanism.

All code in my posts, unless a source is explicitly mentioned, is my own.
 
Don't get me started about those stupid light bulbs.
 
subject: Overloading (primitive/Wrapper) var-arg methods
 
Similar Threads
Var Arguments
Overloading For int and long ????
Overloaded varargs param doubt
Overloaded method
Overloading Ambiguously