Geeks Talk

Prepare for your Next Interview




Which constructor will be called in main

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[]) { ...


Go Back   Geeks Talk > Software Development > OOPS

Register FAQ Members List Calendar Mark Forums Read

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 03-12-2007
Junior Member
 
Join Date: Jul 2006
Location: Hyderabad
Posts: 3
Thanks: 2
Thanked 3 Times in 1 Post
sandhyatammala is on a distinguished road
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
Reply With Quote
The Following 3 Users Say Thank You to sandhyatammala For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 03-13-2007
Expert Member
 
Join Date: Nov 2006
Location: Hyd-IND
Posts: 523
Thanks: 1
Thanked 55 Times in 46 Posts
sutnarcha is on a distinguished road
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-
Reply With Quote
  #3 (permalink)  
Old 03-15-2007
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 28 Times in 27 Posts
kalayama is on a distinguished road
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!"
Reply With Quote
  #4 (permalink)  
Old 03-15-2007
Junior Member
 
Join Date: Feb 2007
Location: Conway, AR, USA
Posts: 23
Thanks: 0
Thanked 4 Times in 3 Posts
rachilc is on a distinguished road
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
Reply With Quote
The Following 2 Users Say Thank You to rachilc For This Useful Post:
  #5 (permalink)  
Old 03-20-2007
Contributing Member
 
Join Date: Mar 2007
Location: renukoot(sonbhadra)
Posts: 37
Thanks: 5
Thanked 3 Times in 3 Posts
rohit dwivedi9450 is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to rohit dwivedi9450 For This Useful Post:
  #6 (permalink)  
Old 06-06-2007
Contributing Member
 
Join Date: May 2007
Location: bangalore
Posts: 65
Thanks: 3
Thanked 8 Times in 6 Posts
hari.nattuva is on a distinguished road
Re: Which constructor will be called in main

It will give the compilation error definetly..............
Reply With Quote
  #7 (permalink)  
Old 06-07-2007
Junior Member
 
Join Date: May 2007
Location: Mysore
Posts: 16
Thanks: 1
Thanked 1 Time in 1 Post
umeshap is on a distinguished road
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 .
Reply With Quote
  #8 (permalink)  
Old 06-07-2007
Junior Member
 
Join Date: Dec 2006
Location: chennai
Posts: 13
Thanks: 4
Thanked 3 Times in 3 Posts
daisy_deepa is on a distinguished road
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.
Reply With Quote
  #9 (permalink)  
Old 06-20-2007
Junior Member
 
Join Date: Jun 2007
Location: USA
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
Surekha venkat is on a distinguished road
Re: Which constructor will be called in main

hi
constructor with string argument will be called in main.
Reply With Quote
  #10 (permalink)  
Old 06-26-2007
Junior Member
 
Join Date: Jan 2007
Location: India
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rajkr11 is on a distinguished road
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.
Reply With Quote
  #11 (permalink)  
Old 06-27-2007
Junior Member
 
Join Date: Jun 2007
Location: pune
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
satishk is on a distinguished road
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.
Reply With Quote
  #12 (permalink)  
Old 07-03-2007
Junior Member
 
Join Date: Apr 2007
Location: Chennai
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
vinotha is on a distinguished road
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.
Reply With Quote
  #13 (permalink)  
Old 09-06-2007
Junior Member
 
Join Date: Sep 2006
Location: Pune, India
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
lhariPrasad is on a distinguished road
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 ! ! !
Reply With Quote
  #14 (permalink)  
Old 09-16-2007
Junior Member
 
Join Date: Aug 2007
Location: India
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
RajeshkumarDey is on a distinguished road
Re: Which constructor will be called in main

Compile time error will be caused.
Reply With Quote
  #15 (permalink)  
Old 09-24-2007
Junior Member
 
Join Date: Jun 2007
Location: India
Posts: 20
Thanks: 0
Thanked 6 Times in 4 Posts
deeptiagrawal is on a distinguished road
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);"
Reply With Quote
  #16 (permalink)  
Old 12-29-2007
Junior Member
 
Join Date: Dec 2007
Location: Erode
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sureshperiasamy is on a distinguished road
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
Reply With Quote
  #17 (permalink)  
Old 01-03-2008
Contributing Member
 
Join Date: Nov 2007
Location: bangalore
Posts: 54
Thanks: 6
Thanked 3 Times in 3 Posts
rahulvegi is on a distinguished road
Re: Which constructor will be called in main

it gives an error because the constructor does not pass null values
Reply With Quote
  #18 (permalink)  
Old 05-22-2008
Junior Member
 
Join Date: May 2008
Location: new delhi
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
gogia_rajat is on a distinguished road
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 .....
Reply With Quote