Prepare for your Next Interview
This is a discussion on Which constructor will be called in main within the OOPS forums, part of the Software Development category; 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[]) { ...
|
|||
|
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 |
| The Following 3 Users Say Thank You to sandhyatammala For This Useful Post: | ||
| Sponsored Links |
|
|||
|
Re: Which constructor will be called in main
I guess,
It looks for a default constructor in the class 'A'. As it is not their, it doesn't call any of the 2 constructors because they need a not null argument for its parameter. ![]()
__________________
Lack of WILL POWER has caused more failure than lack of INTELLIGENCE or ABILITY. -sutnarcha- |
|
|||
|
Re: Which constructor will be called in main
Hi sandhya. 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
__________________
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR] Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!" |
|
|||
|
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 |
| The Following 2 Users Say Thank You to rachilc For This Useful Post: | ||
|
|||
|
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 |
| The Following User Says Thank You to rohit dwivedi9450 For This Useful Post: | ||
|
|||
|
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
it will definetly give compilation error.If u r using new operator it supposed to invoke default constructor.Coz null is not a parameter.its a string literal.
|
|
|||
|
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
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
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 ..... |