| Author |
Method invocation with null argument
|
Alexan Kahkejian
Ranch Hand
Joined: Apr 30, 2003
Posts: 74
|
|
Hi all This code is small part of Dan's Questions: This code prints "String". Why? So how we can know that which overloaded method will be invoked when the argument is null. Thanx in advance Alexan [ July 01, 2003: Message edited by: Alexan Kahkejian ]
|
Alexan Kahkejian<br />SCJP<br />SCWCD<br /><a href="http://www.javaemployer.com" target="_blank" rel="nofollow">http://www.javaemployer.com</a>
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
The most specific method is invoked. In this case, the method taking a String argument is more specific than a method taking an Object argument. Please see the following posts which deal with the same topic: http://www.coderanch.com/t/239328/java-programmer-SCJP/certification/null-argument-method-being-passed http://www.coderanch.com/t/239534/java-programmer-SCJP/certification/null-arg-overloaded-method Also check out JLS 15.12.2.2 Choose the Most Specific Method to find out about how methods are resolved.
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Aravind Elango
Greenhorn
Joined: Jun 02, 2003
Posts: 20
|
|
Hi, The String class is a subclass of the object. When the method m is called with null, the subclass gets called. I guess this could be a general rule that a subclass gets called before superclass.
|
 |
Brian Joseph
Ranch Hand
Joined: May 16, 2003
Posts: 160
|
|
|
What I do, and I hope this is basically the right approach, is look at type of the argument/s and ask myself which one can be implictly cast to all the others. If there is a tie between two or more, then it is a compile error (ambiguous).
|
 |
 |
|
|
subject: Method invocation with null argument
|
|
|