Win a copy of Functional Reactive Programming this week in the Other Languages 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: 50277
80
  • 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: 50277
80
  • 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