This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization Blocks

 
Marjan Stefanoski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please someone explain to me why initialization blocks run before ending of the constructor in the class?
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the constructor finishes, the instance should be ready for use, which includes any initialization.
 
Marjan Stefanoski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic