Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing instance members in constructors

 
Mac Sta. Maria
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read in K&B6 that instance members are accessible only after the super constructor runs.

I stumbled upon the following code:


and it prints:



My question would be:

If instance members are accessible only after the super constructor runs, then why is it that upon execution of the printIt( ) method of class Parent, (which in fact is Child's printIt( ) due to polymorphism), it was able to access the uninitialized instance variable i of Child even though the constructor of Parent has not yet finished executing?

Confused here. What am I missing?
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rather than using word "Accessing", you can say "initialized", the instance variables are initialized after super constructor finishes and before completion of rest of its own(this) constructor.
 
s ravi chandran
Ranch Hand
Posts: 346
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe object creation is a two step process. Firstly, the declared variables are processed by the compiler, then the second step involving calling of super constructors up in the hierarchy. after the initialization from all the super classes, the current instance value should be assigned.
 
Mac Sta. Maria
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for clarifying. Makes sense now.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

same as

in this context this means Child object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic