wood burning stoves 2.0*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Creation Life Cycle" Watch "Object Creation Life Cycle" New topic
Author

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


thanks..
John Jai
Bartender

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..
 
 
subject: Object Creation Life Cycle