“Static initializer” in Java

See the following picture, then go ahead and take a look on the explanation at the end.

2016-07-06_23-18-35

Looking at the picture above, you can see a static block in JavaApplication class. It has no return statement, no argument, no this or super here.

This static block is a “static initializer”, It’s automatically invoked when the class is loaded on the first times (and ONLY ONE) by virtual machine, and there’s no other way to invoke it.

This static block and default constructor are not the same, the constructor will be called everytime we initialize a class. But, the static block  will be called in once (look at the picture).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s