This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance 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
Pie
Posts: 49447
62
  • 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
Pie
Posts: 49447
62
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic