Open In App

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

Last Updated : 30 Nov, 2021
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.



Similar Reads

Instance Initialization Block (IIB) in Java
In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIBs are used to initialize instance variables. So firstly, the constructor is invoked and the java compiler copies the instance initializer block in the constructor after the first statement super(). They run each t
3 min read
Initialization of local variable in a conditional block in Java
Java comprises 5 conditional blocks namely - if, switch, while, for and try. In all these blocks, if the specified condition is true, the code inside the block is executed and vice-versa. Also, Java compiler doesn't let you leave a local variable uninitialized. While initializing local variable inside a conditional block, one must bear the followin
3 min read
Execute main() multiple times without using any other function or condition or recursion in Java
Given task is to execute main() multiple times without using any other function and without recursion() and without error. Given condition is that if executing main() n times then you can only call him (n-1) times. Solution: class Test { // static block static { main(new String[] { "Hello" }); } public static void main(String[] args) { Sy
1 min read
Access Super Class Methods and Instance Variables Without super Keyword in Java
A class describes the property and the attributes of the object. A class contains mainly includes the following components. Modifiers: The keywords in java which provide a set of restriction on the class and its members and methods.Class keyword: The initialization of the class is done by class reserved word and following by the name of the class.C
3 min read
Difference Between Object and Instance in Java
The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class. Object in JavaThe object is an instance of a class. A class is a blueprint or template that describes the behavior and properties of the objects of the
3 min read
Initialize a static Map in Java using Double Brace Initialization
In this article, a static map is created and initialised in Java using Double Brace Initialization. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Double Brace Initialization In Double Brace Initialization: The first brace creates a new Anonymous
2 min read
Java Program to Demonstrate the Nested Initialization For Singleton Class
A Singleton Class is capable of producing just a single instance. Every Singleton class has a getInstance method which returns its object. When the getInstance method is called for the first time, an object of the class is generated, stored, and then returned. On subsequent calls to getInstance, the same object generated earlier gets returned. Nest
4 min read
Java Program to Demonstrate the Non-Lazy Initialization Thread-Safe
Java is one of the most popular Object-Oriented programming languages offered by Oracle which has a wide range of applications in website and application development explored by developers. Java along with some well-known and robust frameworks like Spring, Spring-Boot, etc. makes developer's life easy while developing/coding Software. Java offers 2
6 min read
Java Program to Demonstrate the Lazy Initialization Thread-Safe
Java is a popular object-oriented programming language used by developers and coders for website/app development. The creation of a class object using a new keyword is referred to as object instantiation. Java by default allows users to define two ways of object instantiation which are Eager and Lazy. Java Interpreter interprets program/script code
8 min read
Java Program to Demonstrate the Lazy Initialization Non-Thread-Safe
In recent years object-oriented programming has formed the pillars/bases of Website and Application (Software) Development. Java and Python are popular Object-Oriented programming languages out of which the former one is offered and maintained by Oracle, while the latter one is open-source. Java along with robust and easy-to-setup frameworks like s
8 min read
Article Tags :
Practice Tags :