Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Creation Life Cycle

 
Amitosh Mishra
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation John..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic