< Prev
Next >



Instance Initialization Blocks in Java





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




Instance Initialization Block


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

class A
{

int a;

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

public static void main(String... ar)
{
A ob = new A(); 	//instance initialization block is called automatically as constructor is called.
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 called only when you call the constructor


As 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 instance initialization block won't be executed.

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);
}

}

We have defined an instance initialization block in the class A, but it isn't called yet as we haven't called constructor of class A to create its object. Hence, you won't see anything in output when you run this program.




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.

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 super() . Hence -
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 static variables and static method of its class, an instance initialization block can access both instance variables and static variables of its class.


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 -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Static Initialization Block
Static vs Instance Block>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2