aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method invocation with null argument Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Method invocation with null argument" Watch "Method invocation with null argument" New topic
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).
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Method invocation with null argument
 
Similar Threads
can "null" be considered as an object?
invocations prob::
error in system.out.println
Passing null as a method argument
overloading doubt