File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes re: ambiguous method calls 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 "re: ambiguous method calls" Watch "re: ambiguous method calls" New topic

re: ambiguous method calls

Ivan Karamazov

Joined: Dec 30, 2006
Posts: 6
I could use some clarification on this issue...according to Q10 on, the following code compiles fine...

public class Boxing10 {
public static void main(String[] args) {
int i = 10;
int k = 20;
static void method(Integer... i){
System.out.println("Integer varargs is called");
static void method(Integer i,Integer j){
System.out.println("Integer,Integer is called");
static void method(int... i){
System.out.println("int varargs is called");

However, based on some other mocks this seems wrong. According to the Inquisition program I downloaded, the following code won't compile.

class test {
public static void main(String[] args) {
test inst_test = new test();
inst_test.method ( 1 , 1 , 1);
inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
inst_test.method ( 1 , new Integer(5) );
inst_test.method ( new Integer(10) , 1 );
public void method( Integer... I ){
System.out.println("Eye in the sky");
public void method( int... i ){
System.out.println("Fly in the pie");

Explanation given is "To the compiler Integer... and int... are pretty much the same. This results in an ambiguous state, which results in an error. The JVM wont know which method to call if this is allowed to be compiled." It seems like I have seen a similar complaint about ambiguous calls on other questions and material. Which one is correct? Does it have to do with the first example using static methods?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
No, it's not because they are static.

The reason is that in order to make the new JVMs compatible with older code, method matching is done in phases.

The first phase is to see if there are any matches without using boxing or varargs.

If there are none, then the second phase is to see if there are any matches using boxing but not varargs.

If there are none, then the third phase is to see if there are any matches using boxing and varargs.

In the first example, there is match using boxing.

In the second example, you have two methods which will match the call but neither is more specific than the other.
Ivan Karamazov

Joined: Dec 30, 2006
Posts: 6
Nevermind, just realized what was going on. I need to take a break.

EDIT - Thanks for the reply, that got slipped in quickly.
[ April 05, 2007: Message edited by: Ivan Karamazov ]
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
what about this rxample , why its give same error with int...

as i know it will choose the narrowest method which is Integer not long or float or double !

I agree. Here's the link:
subject: re: ambiguous method calls
It's not a secret anymore!