• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

this,super in constructors

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: GetCertified Exam Lab/Q no. 48 / Practice Exam 1



I thought the answer would be caabed but it runs to be cabed.
Is there a rule that, if for an object, a constructor is once run, it will not run again? (because, I thought the second a in the output comes because of the this() call in A(int i) )
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rekha Srinath:
Source: GetCertified Exam Lab/Q no. 48 / Practice Exam 1



I thought the answer would be caabed but it runs to be cabed.
Is there a rule that, if for an object, a constructor is once run, it will not run again? (because, I thought the second a in the output comes because of the this() call in A(int i) )


You are clear with that A class will be initialized first and C gets printed.
Now
When test() object gets created -
It calls Test constructor that would call super default constructor in the case you dont provide "this". Since you've mentioned "this" in the test constructor it will call test(int p) that in turns calls super(p)that calls super() before doing anything in super(p). later super(p) gets executed -> test(p) gets executed -> test() itself.

I hope it would help.
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets go through the flow here

new test(); calls

test()
{
this(89);
System.out.print("d");
}

the this(89) call calls

test(int p)
{
super(p);
System.out.print("e");
}

super(p); call calls

A(int i)
{
this();
System.out.print("b");
}

this() call calls

A()
{
super();
System.out.print("a");
}

super() call calls constructor of Object class which prints nothing. Now the System.out.print("c"); in instance initializer block gets executed then System.out.print("a"); gets executed. the control will return back to

A(int i)
{
this();
System.out.print("b");
}

System.out.print("b"); is executed and control returns back to

test(int p)
{
super(p);
System.out.print("e");
}

System.out.print("e"); is executed then control returns back to

test()
{
this(89);
System.out.print("d");
}

So finally d is displayed. Now I think there is nothing happening like what you said. If you still have doubt please clarify more what doubt it is...
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patricia & Ankit,

The biiiiig mistake that I did was I missed out this point: (quoting from Patricia)

t calls Test constructor that would call super default constructor in the case you dont provide "this". Since you've mentioned "this" in the test constructor it will call test(int p).......



What I was wrongly doing was this::: I was jumping directly to A() when I hit the new test() call, since A is the superclass of test. But now I understood that the flow will directly go to A() only in such a case where there is no this() or super() in the subclass.

Thanks both of you for the explanations
reply
    Bookmark Topic Watch Topic
  • New Topic