Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Which constructor will be called in main

  1. #1
    Junior Member
    Join Date
    Jul 2006
    Answers
    1

    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


  2. #2
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    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-

  3. #3
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    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!"

  4. #4
    Junior Member
    Join Date
    Feb 2007
    Answers
    23

    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


  5. #5
    Contributing Member
    Join Date
    Mar 2007
    Answers
    34

    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


  6. #6
    Contributing Member
    Join Date
    May 2007
    Answers
    60

    Re: Which constructor will be called in main

    It will give the compilation error definetly..............


  7. #7
    Junior Member
    Join Date
    May 2007
    Answers
    15

    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 .


  8. #8
    Junior Member
    Join Date
    Dec 2006
    Answers
    9

    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.


  9. #9
    Junior Member
    Join Date
    Jun 2007
    Answers
    9

    Re: Which constructor will be called in main

    hi
    constructor with string argument will be called in main.


  10. #10
    Junior Member
    Join Date
    Jan 2007
    Answers
    2

    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.


  11. #11
    Junior Member
    Join Date
    Jun 2007
    Answers
    7

    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.


  12. #12
    Junior Member
    Join Date
    Apr 2007
    Answers
    15

    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.


  13. #13
    Junior Member
    Join Date
    Sep 2006
    Answers
    9

    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 ! ! !


  14. #14
    Junior Member
    Join Date
    Aug 2007
    Answers
    2

    Re: Which constructor will be called in main

    Compile time error will be caused.


  15. #15
    Junior Member
    Join Date
    Jun 2007
    Answers
    20

    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);"


  16. #16

    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


  17. #17
    Contributing Member
    Join Date
    Nov 2007
    Answers
    46

    Re: Which constructor will be called in main

    it gives an error because the constructor does not pass null values


  18. #18
    Junior Member
    Join Date
    May 2008
    Answers
    4

    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 .....


  19. #19
    Junior Member
    Join Date
    May 2008
    Answers
    7

    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.



  20. #20
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    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


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact