*
The moose likes Beginning Java and the fly likes Initialization Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initialization Blocks" Watch "Initialization Blocks" New topic
Author

Initialization Blocks

Marjan Stefanoski
Greenhorn

Joined: Mar 29, 2011
Posts: 2
Please someone explain to me why initialization blocks run before ending of the constructor in the class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Welcome to the Ranch

That was obviously a decision they had to make when developing Java™, and the details will be in the Language specification somewhere.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When the constructor finishes, the instance should be ready for use, which includes any initialization.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Marjan Stefanoski
Greenhorn

Joined: Mar 29, 2011
Posts: 2
Thanks for the directions guys,

I found the answer to my question. If some initialization blocks exists in a class run after super(); in the constructor, and this is like that because of the need of having initialization code that all constructors in the class share. So no mater with which constructor we'll make an instance, the initialization blocks will run and initialize the new object.





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Marjan Stefanoski wrote:Thanks for the directions guys,

I found the answer to my question. If some initialization blocks exists in a class run after super(); in the constructor, and this is like that because of the need of having initialization code that all constructors in the class share. So no mater with which constructor we'll make an instance, the initialization blocks will run and initialize the new object.
I can't understand that. The full details are in the Java™ Language Specification. Scroll down from this section to the following §12.5.
 
 
subject: Initialization Blocks