Thursday, July 4, 2024
HomeLanguagesJavaHow to Execute Instance Initialization Block (IIB) without Creating Object in Java?

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

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\Lazyroar Java>javac GFG.java
C:\Users\Bikash\Desktop\Lazyroar 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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments