Monday, 29 February 2016

Initializer block in Java

If you want to initialize an instance variable you will put that code in a constructor. There is one alternative to using a constructor to initialize instance variables which is called initializer block.

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword.

General form of Initializer block

{
    // whatever code is needed for initialization goes here
}

Usage

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors. Common code, that is used by all the constructors can be put in the initializer block.

Example code

public class InitBlockDemo {
 // no-arg constructor
 InitBlockDemo(){
  System.out.println("no-arg constructor invoked");
 }
 // constructor with one param
 InitBlockDemo(int i){
  System.out.println("constructor with one param invoked");
 }
 // initializer block
 {
  System.out.println("This will be invoked for all constructors");
 }
 public static void main(String[] args) {
  InitBlockDemo ibDemo = new InitBlockDemo();
  InitBlockDemo ibDemo1 = new InitBlockDemo(10);

 }
}

Output

This will be invoked for all constructors
no-arg constructor invoked
This will be invoked for all constructors
constructor with one param invoked

Here it can be seen that the code with in the initializer block is copied in every constructor and that code is executed when the constructor of the class is called.

That's all for this topic Initializer block in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. static block in Java
  2. Constructor chaining in Java
  3. Constructor in Java
  4. Constructor overloading in Java
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

3 comments:

  1. This sentence is wrong "Here it can be seen that initializer block is called first before calling the constructor of the class.".

    Initializer block always gets called after calling the constructor only.

    ReplyDelete
    Replies
    1. Thanks for pointing it out... I'll re-frame it what I meant was code to print in initializer block is called first when the constructor is called but what I have actually written is giving the wrong impression..
      In the line above it I did mention "The Java compiler copies initializer blocks into every constructor."
      Many Thanks for pointing it out..

      Delete
    2. Harshit Shrivastava2 December 2016 at 22:24

      Your welcome!!
      For better understanding, you should give an example which creates a base class with a constructor printing something & extend it. Now call base class constructor via super keyword, you will see that after super(); keyword only, your initializer block gets executed. This will prove the words "The Java compiler copies initializer blocks into every constructor."


      Something like

      InitBlockDemo(){
      super();
      System.out.println("no-arg constructor invoked");
      }
      But thanks for the tutorials which are excellent presented in a brilliant way.

      Delete