Instance Initializer Block

Instance Initializer Block


In Java programming language the instance initializer block is used to initialize the instance data member of a class. Whenever an object is created in the main method the instance initializer block is run. In the instance initializer block we can do the initialization of the instance variable directly. But while initializing the instance variable in the instance initializer block we can also perform some extra operations in the initializer block.


Q. What is the use of instance initializer block while we can directly assign a value in instance data member? For example:

class Car {

int velocity = 50;

}


Why use initializer block?

The initializer block is used to perform some extra operations for example the programmer is initializing a data member in the block and also want a loop to traverse or to fill an array of numbers. He can also perform error handling etc.


Example of instance initializer block:

Consider the following example in which there is an instance initializer block and it performs initialization:

CODE:

class Car {

int velocity;

Car () {

system. out. println (“Velocity is” + velocity);

}

{

velocity = 50;

}

public static void main (string args []) {

Car c1 = new Car ();

Car c2 = new Car ();

}

}

OUTPUT:

Velocity is 50

Velocity is 50

In the above example, the instance initializer block is used to initialize the data member “velocity”. In the main () method two object are created and each time an object is created the constructor of the current class will be called and the instance initializer block will also be called and will initialize the value of the data member “velocity”. As two objects are created therefore, the constructor and the initializer block will be called two times and the message will be printed two times.

In Java programming language we can perform operations in three different places:

  1. method
  2. constructor
  3. block

In methods or functions we can perform operations, in the constructors of the classes we can perform operations and in the initializer block we can perform operations.


What is invoked first, instance initializer or constructor?

Consider the following example in which it is demonstrated that which block will be invoked or called first instance initializer or constructor:

CODE:

class Car {

int velocity;

Car () {

system. out. println (“Constructor is invoked”);

}

{

system. out. println (“Instance initializer block is invoked”);

}

public static void main (string args []) {

Car c1 = new Car ();

Car c2 = new Car ();

}

}

OUTPUT:

Instance initializer block is invoked

Constructor is invoked

Instance initializer block is invoked

Constructor is invoked

In the code above, it looks like the instance initializer block is called first but this is not right. When the object is created, the instance initializer block is invoked. The compiler of the java programming language copies the instance initializer block in the constructor but after super () that is added by the compiler implicitly in the constructor as the first statement. Therefore, the constructor will be invoked first and in the constructor the instance initializer block is invoked. The code of the instance initializer block is copied in every constructor by the compiler of java programming language.

After the compilation of the above code the code will look like the following:

CODE:

class Car {

int velocity;

Car () {

super ();

{system. out. println (“Instance initializer block is invoked”); }

system. out. println (“Constructor is invoked”);

}

public static void main (string args []) {

Car c1 = new Car ();

Car c2 = new Car ();

}

}

It is clear that super () is added in the constructor as the first statement and then the instance initializer block after these there comes the actual statements of the constructor.


Rules for instance initializer block:

In java programming language there are three rules that should be abide by for the instance initializer block:

  1. When the instance of the class is created at the same time the instance initializer block is created.
  2. The instance initializer block is called or invoked after the constructor of the parent class is invoked.
  3. The instance initializer blocks are executed in sequence that is in the order in which they appear.


Program of instance initializer block that is invoked after super ():

Consider the following example in which the instance initializer block is invoked after super ():

CODE:

class C {

C () {

system. out. println (“Constructor of parent class is invoked”);

}

}

class D extends C {

D () {

super ();

system. out. prinln (“Constructor of child class is invoked”);

}

{system. out. println (“Instance initializer block is invoked”); }

public static void main (string args []) {

C c1 = new C ();

}

}

OUTPUT:

Constructor of parent class is invoked

Instance initializer block is invoked

Constructor of child class is invoked

In the above example, the constructor of the parent class is called first then constructor of the child class in which after super () the instance initializer block is invoked then the other statements of the child constructor are executed.