[Logo]
Forums Register Login
Initialization Blocks
Please someone explain to me why initialization blocks run before ending of the constructor in the class?
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.
When the constructor finishes, the instance should be ready for use, which includes any initialization.
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.





 

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.
Ever since I found this suit I've felt strange new needs. And a tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS


This thread has been viewed 570 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 18, 2018 19:25:33.