aspose file tools*
The moose likes Beginning Java and the fly likes Order of Static initialization Block,Instance initialization Block and Constructor.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Order of Static initialization Block,Instance initialization Block and Constructor...." Watch "Order of Static initialization Block,Instance initialization Block and Constructor...." New topic
Author

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

Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

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

Joined: Aug 25, 2008
Posts: 149

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Order of Static initialization Block,Instance initialization Block and Constructor....