Hello World ! — {2}

Abhishek Dubey
4 min readAug 6, 2018

--

Initializer block Vs Static block Vs Constructor in Java

Hello Javadevelovers,

In the previous post we discussed about when an initializer block, constructor or static block gets called, have a look at that post also if you haven’t read it yet. And in this post we will discuss on when should we use which among these three.

Let’s start the discussion with:

Constructor : Constructors are used to create and initialize objects. Whenever we use new keyword to create an object, a constructor gets called. If we do not implement any constructor, Java provides a default constructor to create objects. Every object initiated by using a constructor gets its own copy of class variables called instance variables.

Observe the following code snippet:

As we run it, it outputs to:

In the above example we created two objects first and second from the same class and made a change to first’s instance variable. This change made to first’s instance variable did not affected the second’s instance variable because as mentioned earlier objects initialized using constructor have their own copy of class variables. So if you don’t want changes in one object’s variable to affect any other object’s variable, you can use constructors.

Next is Static block : In the code snippet shown above let’s make the variable i static. The updated code snippet would look like:

And the updated output is:

It’s different than our previous output! without even changing anything in the main method. why? because making i static makes it a class level variable and now its getting shared among the objects. So because both objects first and second are now referring to the same i, changes made to one object gets reflected to the other objects. So, for static variables object initiation using constructor may lead to improper or unpredictable code behaviors.

Therefore, to initialize a static variable, a more appropriate way would be using a static block. Code snippet with static block will look like:

Yes, no constructors! because i now will get initialized at the time of class loading and can be accessed anywhere just by the class name as shown in the code snippet. This static block will get executed only once for a class at the time of class loading. So for anything that we want to initialize only once per class and can be shared across objects we can use static block.

And the last one to discuss is Initializer block : Any part of the code that should get executed or initialized for every object can be implemented inside an Initializer block.

For better understanding, let’s observe the following code snippet:

Following is the output when we execute the above code:

Above output implies that irrespective of the constructor type, Initializer block gets executed every time we created an object. So for any common code that we want to execute before and for every object creation, we can use initializer block.

So, ya that’s it! this was the post on some use cases for choosing any one among constructor, static block and initializer block. Once you understand them you can actually use them in combination also.

Please tell me in the comments if you liked the posts or if you have any doubts. Also, if you want me explore and write a posts on something, please mention it in the comments.

Follow me to receive updates about my posts and see all my posts here.

Thank you,

Abhishek Dubey

--

--