Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java Instance initializers? [duplicate]

Tags:

java

What is the sense of "Instance Initializers" in Java ?
Can't we just put that block of code at the beginning of the constructor instead?

like image 783
Ahmed Waheed Avatar asked Jul 20 '11 14:07

Ahmed Waheed


People also ask

What are instance initializers in Java?

Instance initializer block works are used to initialize the properties of an object. It is invoked before the constructor is invoked. It is invoked every time an object is created.

Why do we need instance initializer block?

Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is created. The initialization of the instance variable can be done directly but there can be performed extra operations while initializing the instance variable in the instance initializer block.

What is difference between instance initializer block and constructor?

Initializer block : contains the code that is always executed whenever an instance is created. It is used to declare/initialize the common part of various constructors of a class. Constructors : are used to initialize the object's state.

Why do we use initializer block in Java?

In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created.


2 Answers

I use them very often, typically for creating and populating Map in one statement (rather than using an ugly static block):

private static final Map<String, String> CODES = new HashMap<String, String>() {     {         put("A", "Alpha");         put("B", "Bravo");     } }; 

One interesting and useful embellishment to this is creating an unmodifiable map in one statement:

private static final Map<String, String> CODES =      Collections.unmodifiableMap(new HashMap<String, String>() {     {         put("A", "Alpha");         put("B", "Bravo");     } }); 

Way neater than using static blocks and dealing with singular assignments to final etc.

And another tip: don't be afraid to create methods too that simplify your instance block:

private static final Map<String, String> CODES = new HashMap<String, String>() {     {         put("Alpha");         put("Bravo");     }      void put(String code) {         put(code.substring(0, 1), code);     } }; 
like image 125
Bohemian Avatar answered Oct 04 '22 13:10

Bohemian


You could indeed put the code at the beginning of every constructor. However, that's precisely the point of an instance initializer: its code is applied to all constructors, which can be handy if you have many constructors and a bit of code that is common to all of them.

(If you're just starting out with programming, you might not have known that it is possible to create many constructors for the same class (as long as they take different parameters); this is known as constructor overloading. If you only have one constructor, then an instance initializer is indeed not very useful (Edit: Unless you abuse it in creative fashions, as illustrated in the other answers).)

like image 35
Aasmund Eldhuset Avatar answered Oct 04 '22 14:10

Aasmund Eldhuset