Similar to static initializer blocks, instance initializer blocks are used for adding code which should be executed when an instance of class is created. If present, these code blocks are always executed irrespective of whether a constructor is present in the class or not. The static block looks like:

static{
//your code here
}

An instance initializer block will look like:

{
//your code here
}

When multiple initializer blocks of code are present in a class, they are executed in the order of their appearance. A program showing the use of these instance level blocks follows:

package com.example;

public class Test {

	{
		System.out.println("instance block 1");
	}

	{
		System.out.println("instance block 2");
	}

	public static void main(String[] args) {
		Test t = new Test();
	}
}

Output:

instance block 1
instance block 2

Uses of object initializer

Usually one should never require instance code blocks because of the presence of constructor in all classes. But these code blocks are useful in following scenario:

a) When constructors are overloaded, usually another constructor is invoked with the help of first line looking like this(…). This way, the code from one constructor can be reused. But when there are more than say 5 constructors, it may not be possible (due other conditions like calling super class constructor). In such cases instance initializer blocks can come handy and help in avoiding duplicate code.

b) Constructors in Java don’t have the right to throw or specify exceptions. When there is a requirement for adding exception handling in initialization code, these code blocks can be used.

c) In case anonymous inner classes, instance level code blocks are also very helpful.

d) If the requirement is to execute a piece of code before constructor invocation, these blocks can be used. The initializer blocks are executed before any constructor from that class gets executed (even before invoking super constructor chain).

Thus effectively, these code blocks are similar to having an init method in the life-cycle of an object. This init method is automatically invoked for each object being created for that class.

Instance initializer blocks and constructor in Java admin Core Java
Similar to static initializer blocks, instance initializer blocks are used for adding code which should be executed when an instance of class is created. If present, these code blocks are always executed irrespective of whether a constructor is present in the class or not. The static block looks like: static{ //your...
Similar to <a href="http://www.javaexperience.com/common-reasons-for-non-static-variable-cannot-be-referenced-from-a-static-context/" title="Common reasons for non-static variable cannot be referenced from a static context">static</a> initializer blocks, instance initializer blocks are used for adding code which should be executed when an instance of class is created. If present, these code blocks are always executed irrespective of whether a constructor is present in the class or not. The static block looks like: 1 An instance initializer block will look like: 1 When multiple initializer blocks of code are present in a class, they are executed in the order of their appearance. A program showing the use of these instance level blocks follows: 1 <strong>Output:</strong> <blockquote> instance block 1 instance block 2 </blockquote> <h2>Uses of object initializer</h2> Usually one should never require <strong>instance code blocks</strong> because of the <a href="http://www.javaexperience.com/why-no-constructor-defined-in-servlet/" title="Why no constructor is defined in Servlet">presence of constructor</a> in all classes. But these code blocks are useful in following scenario: a) When constructors are overloaded, usually another constructor is invoked with the help of first line looking like this(...). This way, the code from one constructor can be reused. But when there are more than say 5 constructors, it may not be possible (due other conditions like calling super class constructor). In such cases instance initializer blocks can come handy and help in <strong>avoiding duplicate code</strong>. b) Constructors in Java don't have the right to <strong>throw or specify exceptions</strong>. When there is a requirement for adding exception handling in initialization code, these code blocks can be used. c) In case <strong>anonymous inner classes</strong>, instance level code blocks are also very helpful. d) If the requirement is to <strong>execute a piece of code before constructor invocation</strong>, these blocks can be used. The initializer blocks are executed before any constructor from that class gets executed (even before invoking super constructor chain). Thus effectively, these code blocks are similar to having an init method in the life-cycle of an object. This init method is automatically invoked for each object being created for that class.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.