• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

Instance Initializer in Java

January 19, 2014 //  by Krishna Srinivasan

Instance initializer in Java is used for initializing the instance variables inside a class. It is a simple block without any modifiers used inside any class. If you declare a instance variable with value, then this value can be overridden in the instance initializer or in the constructor. The order of the execution as follows:

  1. Static initializer
  2. Instance initializer
  3. Constructor
  4. Variable initializer

The very common confusion is that what is difference between instance initializer and constructor and why it is used.

  • If you have a code which needs fancy calculations or exception handling, then you have to use this block since normal initialization will not do. However the same code can be written inside a constructor.
  • You can not explicitly throw exception from this block, since it doesn’t return the control there is no one to handle the exception thrown. You would get the compile time error “initializer does not complete normally”.
  • However, you can write a code which can throw the exceptions, you must handle that exception in each constructor.
  • If your class has the multiple constructors then the common initialization has to be repeated for every constructor. If you write the same code in the initializer, it executes for every constructor call. It is one of the reason why we need instance initializer.
  • Instance initializer executes before the constructor calls.
  • Another advantage of this are to use within the anonymous inner classes, where you can not declare any constructors.

Look at the below example:

package javabeat.net.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class InstanceInitializer {
	int i = 20; //Variable Initializer
	public InstanceInitializer() throws FileNotFoundException,IOException{
		i = 30;
		System.out.println("Constructor Called");
	}
	{
		i = 10;
		System.out.println("Instance Initializer Called");
		File f=new File("test-file.txt");
		FileOutputStream fo=new FileOutputStream(f);
		fo.write(10);
	}
	static{
		System.out.println("Static Initializer Called");
	}
	public static void main (String args[]) throws IOException{
		System.out.println(new InstanceInitializer().i);
	}
}

Output of the above example is:

Static Initializer Called
Instance Initializer Called
Constructor Called
30

I hope the above definition would have cleared your confusion about the instance initializers. If you have any questions, please write it in the comments section.

Category: JavaTag: Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java EE Books
Next Post: Difference Between Throw and Throws in Exception Handling »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact