This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compilation Error... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compilation Error..." Watch "Compilation Error..." New topic
Author

Compilation Error...

Ivaturi Srinivas
Ranch Hand

Joined: Jun 03, 2003
Posts: 42
Why the following program gives Compilation Error?
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
If I replace null with some string "AKP" String version is called.
Please tell me the reason.
Thanks
Srinivas Ivaturi
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi
Java compiler has given sufficiant information..

AQuestion.java:10: reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method method(java.lang.String) in AQuestion match question.method(null);

Ur code has method() in overloaded form.........

public void method(StringBuffer sb) { print("StringBuffer Verion"); }
public void method(String s) { print("String Version"); }

Depending on the parameter wich u will pass while invoking any one of these methods will decide which method has to actually execute....
Now the matter of fact is both StringBuffer and String variable/argument can take null as their value. And therefore Compiler can not decide which overloaded method has to be executed.
I hope this will help :-)


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
From one of Dan's Mock Exam Answer list :

The type of the argument is null and could be converted to any of the types StringBuffer or String by method invocation conversion. Both are applicable, but none of the two is more specific than other. The ambiguity results in a compile-time type error.

If there is no inheritence relationship between this argument objects when we pass the null ,the compiler can not able to find the more specific object , leads to compile time exception .
[ January 13, 2004: Message edited by: Mohamed Yasin ]

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Ashok Paulraj
Ranch Hand

Joined: Jul 07, 2003
Posts: 78
I agree with your explanations, but does the same happen if you pass an int to a method that has got 2 overloaded forms (float & double). Clarify me !!
~ Shalini
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation Error...
 
Similar Threads
Problem with reference
overload question
About String & StringBuffer
doubt from abilash's mock test
About null ...