• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling constructor with null ?

 
Ramana Uppala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the null will be treated as the string reference
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parameswaran,
Can you justify your statement.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic