aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Resolution 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 "Method Resolution" Watch "Method Resolution" New topic
Author

Method Resolution

praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
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

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
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

Joined: Feb 02, 2006
Posts: 2023
Selecting the Most Specific Method
Amar kon
Greenhorn

Joined: Mar 30, 2006
Posts: 10
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

Joined: Feb 27, 2006
Posts: 52
http://www.coderanch.com/t/326225/java/java/Ambiguous-Reference-method
may be this will help


OfCourse Learner...<br />SCJP 5, SCWCD1.4<br />Blogs: <a href="http://sandeepkunwar.blogspot.com" target="_blank" rel="nofollow">http://sandeepkunwar.blogspot.com</a>
praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
Thanks guys, I got it.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Y following code compiles correctly...


Regards

Naseem.K


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
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

Joined: Apr 25, 2005
Posts: 809
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
 
 
subject: Method Resolution