Which constructor will be called in main
Hi frens,
Plzz...give ans to my qn following..
class A
{
String str;
A(A ob)
{
ob.str=str;
}
A(String s)
{
this.s=str;
}}
class Main
{
psvm(String a[])
{
A a=new A(null);
}
}
In the above code, which constructor will be called in main
Thanx & regards,
Sandhya
Re: Which constructor will be called in main
I guess,:confused:
It looks for a default constructor in the class 'A'. :eek:
As it is not their, it doesn't call any of the 2 constructors because they need a not null argument for its parameter. :D
Re: Which constructor will be called in main
Hi [B]sandhya[/B]. have you tried compiling and executing this code? It won't even compile! (I didn't try it in a compiler but it won't compile).
Simply because "null" is not an identifier/keyowrd. So, it will search for a variable names null which it won't find. So, it will throw an error saying "null" not found...
-Kalayama
Re: Which constructor will be called in main
Kalayama is right the above code will not compile.
Here is the reason:
we dont know which constructor to bind your A(null) call to.
Usually the most specific method/construcotr is called.
For example, if you had two constructors
A(Object b){}
and
A(String s){}
the A(String s) would be called for A(null) (Imagine null to be an empty object)
because String is child of Object and hence, is more specific.
But between A and String, one is not more specicifc than other.
There are other mishaps in the code though :)
-> You have defined the STring array passed to main as a. you can't redfeine a inside main again
-> inside the A(String s) you should have this.str = s and not the other way round
hope this helps
Re: Which constructor will be called in main
hi.............>>
kalyama what i think youn are using null in tyhe object in new operator and null is no keyword
so what i think it will go for default constructor what i think as i am just learning java
Re: Which constructor will be called in main
It will give the compilation error definetly..............
Re: Which constructor will be called in main
Sure A(String s) constructor will excecute .
bcoz null is not an object , is an string literal .
Re: Which constructor will be called in main
class A
{
String s;
A(A ob)
{
ob.s=s;
}
A(String s)
{
this.s=s;
}
}
class Main
{
public static void main(String a[])
{
A a1=new A(null);
}
}
During Copilation it will display the following Error:
Reference ro A is ambiguous, both method A(A) in A and method A(String) in A match.
if U use ,
this.str=s; it also produce compilation error.
Re: Which constructor will be called in main
hi
constructor with string argument will be called in main.
Re: Which constructor will be called in main
u can pass any type of argument in constructor and u can created its object in main method of class.
Re: Which constructor will be called in main
it will definetly give compilation error.If u r using new operator it supposed to invoke default constructor.Coz [U]null[/U] is not a parameter.its a string literal.
Re: Which constructor will be called in main
Hi ,
this won't cause any compile time or ru n time error.
it'll run successfully.
Re: Which constructor will be called in main
Sandhya,
First of all, you will get a compilation error, saying that the constructor A(a) is ambiguous if you change the constructor signature as A(Object a) then it will compile and the String constructor will be called, as in Java null is a special literal of the null type, it can be cast to any reference type but not to primitive type. As null cannot be casted to A it will throw ambiguous error. if you try to call A a1=new A((A)null); then it will compile and gets the A(A ob) constructor called.
If you change the Constructor signature as A(Object ob) then the String constructor will be called.
Remember null can be casted to any reference types.
Does this makes sense !
Enjoy java coding ! ! !
Re: Which constructor will be called in main
Compile time error will be caused.
Re: Which constructor will be called in main
hello,
Daisy deepa is correct it will give an compilation error stating : "reference to A is ambiguous, both method A(A) in A and method A(java.lang.String) in A match
A a=new A(null);"
Re: Which constructor will be called in main
here parameterized constructor will be called. because here null is a string . so second (Parameterized) constructor is called
Re: Which constructor will be called in main
it gives an error because the constructor does not pass null values
Re: Which constructor will be called in main
every1 has given their views abt null n ambiguity but.........
there is one more error
constructor cannot take class as its parameter
it can only take its reference .....
Re: Which constructor will be called in main
hi
In this code i think the 2nd constructor will be called bcos null is a string type.
Re: Which constructor will be called in main
Hi I have tried to compile this code,, but it gives me compilation error,,,,
The reason for that as my understanding is:
1. The order to invoke constructor is the most specific to generalize, hence "null" is not an identifier/keyowrd. and then non of constructor find that match ,, so throw an exception