• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ambiguous for Compiler  RSS feed

 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends ,
Can anyone explain reason for the following error
Following code won't Compile
it shows Compile time error

dd.java:2192: reference to dd is ambiguous, both method dd(Object) in dd and met
hod dd(int[]) in dd match
new dd(null);


public class dd
{

dd(Object o)
{

System.out.println("Object ");
}

dd(int[]a)
{

System.out.println("Object 111");
}
public static void main(String []p)
{
new dd(null);

}

}
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


public class dd
{

dd(Object o)
{

System.out.println("Object ");
}

dd(int[]a)
{

System.out.println("Object 111");
}
public static void main(String []p)
{
new dd(null);

}

}


In your code, null can be assigned to Object o or array a. Remember arrays in java are objects.
Since both methods r most specific, compiler can't pick the right method and give error at compilation time.


See JLS - 15.12.2.5 Choosing the Most Specific Method

Selecting the Most Specific Method

---------------------------------------------------------------------


Use java naming convention


regards

Naseem Khan
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here int[] method is more specific, and the code compiles fine in JBuilder on my PC.
 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naseem Khan ,
I got it but I want to know what's
reason behind it ?
can u give answer?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are true...
I compiled it on jdk 1.5 and it compiles correctly and at runtime gives output.... Object 111

array version is more specific than object version coz any java object can be referenced by Object including array.
Object class is MOTHER of everything.

My previous reply was not correct. It won't give any compilation error on 1.5

regards

Naseem

[ May 17, 2006: Message edited by: Naseem Khan ]

[ May 17, 2006: Message edited by: Naseem Khan ]
[ May 17, 2006: Message edited by: Naseem Khan ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swarupa,
If u r still getting compilation error, then plz let me know on which version of java u compiled dis code.

regards

Naseem.K
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!