Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Resolution

 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In the following code,
public class Test {
public static vois main(String args[]){
flipFlop("(String, Integer, int)", new Integer(4), 2004); //1
}
private static void flipFlop(String str, int i, Integer iRef) { //2
out.println(str + " ==> (String, int, Integer)");
}
private static void flipFlop(String str, int i, int j) { //3
out.println(str + " ==> (String, int, int)");
}
}
Why is the method call at 1 gives compiler error? Is it not the method at 3 more specific than method at 2 ? I can find the arguments (String str, int i, byte b ) that can be passed to the method at 3 but not for the method at 2 but I cannot find any arguments that are valid for method 2 and not for 3.

regards
praveen
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static vois main(String args[]){
flipFlop("(String, Integer, int)", new Integer(4), 2004); //1
there is no method definition which is satisfying your parameters ,so error came, in method 2 your formal parameters are string,int,Integer

so write a method with formal parameters String,Integer,int,
first two are string and integer class reference variables ,last is the integer data type.
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess, my question is not clear. If I comment any one of the two methods, then the code compiles fine. However, when both methods are there, the compiler complains that the method call is ambiguous. My question is how can we determine whether a method is more specific than the other?

regards,
Nanda Kishore
 
wise owen
Ranch Hand
Posts: 2023
 
Amar kon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, u said " I can find the arguments (String str, int i, byte b ) that can be passed ". but u have to remember that any literal which is a number is of the type int. and any decimal literal is of the type double. so when u write any literal for ex, 10, 200. they are all ints and not bytes or shorts. so ur method invocation of (String str, int i, byte b )is wrong. and your method invocation by the compiler wil be(String str, int i, int b ).

Amarendra k
 
Sandeep Singh Kunwar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/326225/java/java/Ambiguous-Reference-method
may be this will help
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I got it.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Y following code compiles correctly...


Regards

Naseem.K
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naseem,
that's because of boxing and unboxing. The second argument Integer will be unboxed to int and the third argument which is an int literal will be boxed to an Integer

regards
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great catch...Thanks Praveen

I Compiled this code on j2sdk 1.2.1 and this is the compilation error output....
Incompatible type for method. Can't convert java.lang.Integer to int and int to java.lang.Integer.
I don't know wat will be output on j2sdk 1.4 version.
I think boxing and unboxing is the feature of 1.5, dats y compiler compiles without any error output.
Same is the problem in ur code which u have mentioned in start. It can box and unbox in either way, dats y it says ambiguous call.

regards

Naseem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic