Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Static initialization Block,Instance initialization Block and Constructor....

 
Shanu Pandey
Greenhorn
Posts: 18
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If in a code we have a instance initialization code then that code is executed after the constructor's call to the super() is complete.


The output should be:
START
static - grandparent
static - parent
static - child
constructor - grandparent
instance - grandparent
constructor - parent
instance - parent
constructor - child
instance - child
END

But the actual Output is:

START
static - grandparent
static - parent
static - child
instance - grandparent
constructor - grandparent
instance - parent
constructor - parent
instance - child
constructor - child
END

please make it clear why??
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance initializer needs to
be executed before the constructor returns control.



This is how 'a' gets initialized

And the output is:

StaticTest - static
START
StaticTest - BEGIN - instance
a = 12
StaticTest - END - instance
StaticTest - constructor
a = 12
END


HTH
Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic