• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get confusion in constructor execution

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I thought that the output would come as "YYXZ". But it is "YXYZ". I dont know how it is working. Please explain this.

[ September 05, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated."

In every constructor the first line execution will be calling the default constructor of the base class (super()) or calling another overloaded constructor (this())

so in this when the Class Z initialted , the instance variable 'y' got initiated So now the Y's constructor will called. and then the Z's constructor called , before the execution it calls the x's (base class) constructor like



thats why you got the YXYZ ... hope it will help you
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use [B] tags[/B] to surround formatted code. It makes it so much easier to read.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



you didn't mention why are you expecting YYXZ.
Anyways the flow is as follows:

1.control goes to Z constructor
2.since no super or this keyword is there in Z constructor, control goes to superclass default constructor.
3.Control goes to Object default constructor
4.Now instance variable of X get initialized// prints Y
5.Then control goes to X constructor // prints X
..... and the rest you know as you answered it right.

hope you got it.
 
Kaarthick Ramamoorthy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arul,

I agree with your point. But when


Z()
{
super();
}

1) Z constructor calls super()

2) The super class( X() ) constructor is called. Before executing the (X() ) constructor, it has to initialize it's (X class) Y instance varialble.

3) But in our case, it happens next. Why?

Rule : Instance variables are initialized before the constructor execution.

I need clarification.
 
Kaarthick Ramamoorthy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vaibhav Chauhan ,

Thank you. Thank you.

I understood.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i can't understand the o/p

what i thought is
'Actually instance variables are initialised only after constructor is executed ie only after the object is constructed '

pls clarify
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sriram, think yourself that if constructor has completed its job it means that the object is created successfully but how can an object can be created without initializing its instance variables. Even if we don't initialize it explicitly , instance variables get initialized to their default values.

hope you got it.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic