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

calling constructor with null ?

Ramana Uppala
Greenhorn

Joined: Jan 04, 2005
Posts: 16
The below program calling String Constructor.
null is treated as Object or String reference ?


1. public class PowerSupply
2. {
3. public PowerSupply(String voltage){
4. System.out.println ("PowerSupply(String) executed");
5. }
6. public PowerSupply(Object voltage){
7. System.out.println ("PowerSupply(Object) executed");
8. }
9. public PowerSupply(){
10. System.out.println ("No argument constructor execute");
11. }
12. public static void main(String[] args) {
13. PowerSupply ps = new PowerSupply(null);
14. }
15. }
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
the null will be treated as the string reference
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hi Parameswaran,
Can you justify your statement.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
It will be treated as string reference.
Which method to choose is very well explained in jls,
Specific method

it says that:

Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:


T can be converted to U by method invocation conversion.
Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.
A method is said to be maximally specific for a method invocation if it is applicable and accessible and there is no other applicable and accessible method that is more specific.


Well, in simple terms,
if suppose the constructor "public PowerSupply(Object voltage)" is getting called, u cannot convert Object to String.
But if u call String constructor u can convert String to Object as Object is a super class, so ur String constructor is maximally specific.
Try this example:


U will get a compiler error because even char[] can be converted to Object because Object is a super class of char[], and also String can be converted to Object.
So there are two maximally specific methods which is by itself not possible, there has to be only one, so a compiler error is displayed saying ambiguity in method call.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i am sorry i donno why the null is considered as string but i work out the code like shown below




public class Param
{

public static void main(String args[])
{
Param p=new Param();
Param p1=new Param(new Object());
Param p2=new Param(null);
Param p3=new Param("param");

}

Param()
{
System.out.println("empty");
}

Param(String s)
{
System.out.println("str");
}

Param(Object o)
{
System.out.println("obj");
}

}


The output i got is

empty
obj
str
str

if anybody know why the null is considered as string please share with us
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Parameswaran,
please look at my just previous post.
Hope thats clear to u,
if u have any doubts on this , do let me know
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


The proper answer would be , the more specific method is get called when you have more than one choice .

The above program gives compiler time error because of ambiguity between String & Param .
 
Don't get me started about those stupid light bulbs.
 
subject: calling constructor with null ?