How to Execute Instance Initialization Block (IIB) without Creating Object in Java?
Last Updated :
30 Nov, 2021
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
3 min read
Order of execution of Initialization blocks and Constructors in Java In Java, there are various techniques, which we can use to initialize and perform operations on objects such as methods, constructors, and initialization blocks. These tools are used to ensure that the program works as expected. Instance Initialization Blocks (IIB) are used to initialize instance va
4 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 insi
3 min read
Double Brace Initialization in Java The combination of two separate processes in Java is known as Double Brace Initialization in Java. As the name suggests, there are two braces {{ included in it. A single brace { is nothing new for programmers. The first brace in the double brace initialization is used to create an anonymous inner cl
4 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
3 min read
How to Calculate Size of Object in Java? In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
2 min read