Initialization block Java

Initialization blocks in Java allow you to initialize your instance variables using more complex statements. In this article,  I teach how to use non-static and static initialization blocks in Java classes.

Non-static initialization blocks

Initialization blocks are usually put after instance variables declarations and before constructors, but it is not a rule. Initialization blocks have their body enclosed within { curly braces }.

Take a look at the example code below:

public class Prototyping {
   public static void main(String[] args) {
      Test t = new Test();
      System.out.println(t.number); // prints "123"
      System.out.println(t.text);   // prints "cats"
   }
}

class Test {
   int number;
   String text;

   {
      number = 123;
      text = "cats";
   }

   // The rest of the class
}

The code above contains a very simple initialization blocks that initializes our two instance variables number and text.

Initialization blocks are not limited to single assignments, which makes them very useful. Example of a little more complex initialization block can be found below:

class Test {
   int number;
   String text;

   {
      Random rand = new Random();
      int randomNumber = rand.nextInt(123)+1;
      number = Math.round(randomNumber / 3.14f);

      text = "We have got number " + number;
   }

}

You may be wondering what will happen if we set a variable value both in the instance variable declaration and in the initialization block. The answer is that one value with get overridden, but which one depends on the order in which you put variable declarations and initialization block in your class.

In example, this code will output 2 and init block, because initialization block is placed after instance variables declaration and initialization:

public class Prototyping {
   public static void main(String[] args) {
      Test t = new Test();
      System.out.println(t.number); // prints 2
      System.out.println(t.text);   // prints "init block"
   }
}

class Test {
   int number = 1;
   String text = "declaration";

   {
      number = 2;
      text = "init block";
   }
}

If we will put the initialization block first and the instance variables declaration and initialization as second, we will get other result:

public class Prototyping {
   public static void main(String[] args) {
      Test t = new Test();
      System.out.println(t.number); // prints 1
      System.out.println(t.text);   // prints "declaration"
   }
}

class Test {
   {
      number = 2;
      text = "init block";
   }

   int number = 1;
   String text = "declaration";
}

Now number and text are set to 1 and “declaration”.

Constructor will be always executed as last. Therefore, constructor position in the code doesn’t affect variable initialization:

public class Prototyping {
   public static void main(String[] args) {
      Test t = new Test();
      System.out.println(t.number); // prints 3
      System.out.println(t.text);   // prints "constructor"
   }
}

class Test {
   public Test() {
      number = 3;
      text = "constructor";
   }

   {
      number = 2;
      text = "init block";
   }

   int number = 1;
   String text = "declaration";
}

The code above will output 3 and constructor, despite that we put constructor as first in the class code.

Uninitialized instance variables will be set to their default values. You can use initialization blocks to set final variables as well.

As a general convention, instance variables are being put as first in the code, then initialization blocks, then constructors with rest of code. This is what I recommend to do to make your code more readable for other programmers (and for you as well).

Static initialization blocks

Static initialization blocks work on the same rules as non-static initialization blocks, with this difference, that they work on static variables. To create a static initialization block, use the curly braces as in the previous examples, but with static keyword put before opening brace.

Static initialization blocks are always executed before first access to a static variable.

Simple example of static initialization block below:

public class Prototyping {
   public static void main(String[] args) {
      System.out.println(Test.number); // prints 3
   }
}

class Test {
   static int number;

   static {
      number = 3;
   }
}

Learn more about Java