• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading (primitive/Wrapper) var-arg methods

 
Christian Kindler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic