Open In App

How to Execute Instance Initialization Block (IIB) without Creating Object in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIB are used to initialize instance variables. We know that the instance block is the name-less method in java inside which we can define logic and they possess certain characteristics. Instance block logic is common for all the objects and it will be executed only once for each object during its creation. Now, let us see the normal execution of instance blocks by creating objects.

Demonstration 1: Normal execution of instance block

Java




// Executing instance block
// by creating object.
  
// Class 1
// Helper class
class GFG {
  
    {
        // Creation of an instance block
        System.out.println("Instance block called by creating objects");
    }
}
  
// Class 2
// Main class
class GFGJava {
  
    // main driver method
    public static void main(String[] args)
    {
  
        // Object of 1st kind
        GFG obj1 = new GFG();
  
        // Object of 2nd kind
        GFG obj2 = new GFG();
  
        // Object of 3rd kind
        GFG obj3 = new GFG();
    }
}


Output:

Instance block called by creating objects
Instance block called by creating objects
Instance block called by creating objects

Explanation: Instance block gets executed only once for each object creation. Here it got executed for obj1, obj2, and obj3.

Demonstration 2: Executing instance block without creating an object

In Demonstration 1 we saw the execution of instance block by creating objects which are obvious but there is a misconception that an instance block can not be executed without creating an object which is not true. In our next demonstration, we will see that how can we execute an instance block without creating an object.

Java




// Executing instance
// block by creating object.
  
// Class 1
// Helper class
class GFG {
  
    {
        // Creation of an instance block
        System.out.println(
            "Instance block called by creating objects");
    }
}
  
// Class 2
// Main class
class GFGJava {
  
    // main driver method
    public static void main(String[] args)
    {
        // Declaring instance block inside main method
        {
            System.out.println(
                "Instance block inside main method called without creating an object");
        }
  
        // Object of 1st kind
        GFG obj1 = new GFG();
  
        // Object of 2nd kind
        GFG obj2 = new GFG();
  
        // Object of 3rd kind
        GFG obj3 = new GFG();
    }
}


Output:

C:\Users\Bikash\Desktop\GeeksforGeeks Java>javac GFG.java
C:\Users\Bikash\Desktop\GeeksforGeeks Java>java GFG
Instance block inside main method called without creating an object
Instance block called by creating objects
Instance block called by creating objects
Instance block called by creating objects

Explanation: It is also possible to execute an instance block without creating an object. To execute an instance block without creating an object we need to define it explicitly inside the main method.



Last Updated : 30 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads