Advertisement



< Prev
Next >



Instance Initialization Block in Java





As you must have read about the static initialization block in our previous tutorial, so let us continue with the study of instance initialization block.




Instance Initialization Block


Instance initialization block of a class is associated with its instance/object creation and it is automatically called when you call the constructor of its class for creating an object.

//Java - Example of instance intialization block

class A
{

int a;

//Instance Initialization block
{
	a=10;
	System.out.println("An object is created");
}

public static void main(String... ar)
{
	A ob = new A(); 	//Call to constructor has automatically called 
				//the instance initialization block
	System.out.println(ob.a);
}

}


Output-


An object is created
10





Note :


Instance initialization block does not precede with any keyword or name.




Instance Initialization block is executed before constructor


The instance initialization block of a class is executed before the execution of the constructor of a class.

//Java - Example of instance intialization block

class A
{

//Instance Initialization Block
{	
	System.out.println("Instance initialization block is executed");
}

A()
{
	System.out.println("Constructor is executed");
}

public static void main(String... ar)
{
	//Creating an object of class AA
	A ob = new A();
}

}


Output


Instance initialization block is executed
Constructor is executed





Instance Initialization block is called only when you call the constructor


As you already know by now that, an instance initialization block of a class is associated with its instance/object creation, hence, if you don't call the constructor of the class, the defined instance initialization block won't be executed.

//Java - Example of instance intialization block

class A
{

//Instance Initialization Block
{	
	System.out.println("An object is created");
}

public static void main(String... ar)
{
	int a=10;
	System.out.println(a);
}

}


Output


10

In the last program, we have defined an instance initialization block in the class A, but this instance initialization block isn't called because we haven't called the constructor of class A, to create its object. Therefore, the instance intialization block is not executed when you run this program.


Advertisement




Multiple Instance Initialization Block


You can define multiple initialization blocks within your class and the order(starting from top) in which they are defined is the order in which they are executed, at the time of object creation.

//Java - Example of instance intialization block

class A
{

//First Instance Intialization Block
{	
	System.out.println("An object is created");
}

//Second Instance Initialization Block
{	
	System.out.println("Second notification about the  object creation");
}


public static void main(String... ar)
{
	A ob = new A();
}

//Third Instance Initialiation Block
{	
	System.out.println("Third notification about the  object creation");
}

}


Output-


An object is created
Second notification about the  object creation
Third notification about the  object creation





Instance initialization block in inheritance.


When the constructor of a subclass is called, there is an automatic call by the compiler to the constructor of its superclass, by making a call to the super(), therefore:
//Java - Example of instance intialization block

class B
{

//Constructor of B
B()
{
	System.out.println("Constructor of B is called");
}
}



class A extends B
{

//Constructor of A
A()
{
	System.out.println("Constructor of A is called");
}


//Instance Intialization Block of A
{
	System.out.println("Instance Initialization block is called"); //Call to super() is made before executing this statement.
}

public static void main(String... ar)
{
	A ob= new A();
}

}


Output -


Constructor of B is called
Instance Initialization block is called
Constructor is A called





Instance initialization block can access instance variables and static variable


Unlike static initialization block, which could only access the static variables and the static methods of its class, the instance initialization block can access both the instance variables and the static variables of its class.

//Java - Example of instance intialization block

class A
{
static char ch='a';
int a=20;

//Instance initialization block of B
{
	System.out.println("An object of A is created");
	System.out.println("value of instance variable, a = "+ a);
	System.out.println("value of static character, ch = "+ ch);
}


public static void main(String... ar)
{
	A ob= new A();
}

}


Output-



An object of A is created
Value of instance variable, a = 20
Value of static character, ch = a




Please share this article -





< Prev
Next >
< Static Initialization Block
Static vs Instance Block>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement