This week's book giveaway is in the Game Development forum.
We're giving away four copies of Badass: Making Users Awesome and have Kathy Sierra on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Creation Life Cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Creation Life Cycle" Watch "Object Creation Life Cycle" New topic

Object Creation Life Cycle

Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Hi all,
can you suggest me some tutorial to understand java object creation.

i am trying to understand the response of running the following code

response is

John Jai

Joined: May 31, 2011
Posts: 1776
This is the flow - super class constructor executes and then the instance variables are initialized with the assigned values.

1. So when you create a object of C, B's constructor is called by the compiler inserted super(). So your C class constructor will actually be like below after compilation.

2. Now the B constructor runs and prints "form construcor1 NB". There is a call to print2() method.

3. Though at compile time it looks like a call to print2() of B, during runtime since the actual object created is for C, due to method overriding the print2() in C gets called.

4. Since instance variables are not given values till super class constructor is finished, the instance variable NStatic is still null when print2() is called from B's constructor and hence "from Method2 null" is printed.

5. Super class constructor ends and now instance variables are assigned respective values and hence NC is printed for NStatic field henceforth.
Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Thanks for the explanation John..
I agree. Here's the link:
subject: Object Creation Life Cycle