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. IIBs are executed before constructors. They run each time when object of the class is created.
- Initialization blocks are executed whenever the class is initialized and before constructors are invoked.
- They are typically placed above the constructors within braces.
- It is not at all necessary to include them in your classes.
IIB block Constructor Called
Multiple Instance Initialization Blocks in a Program
We can also have multiple IIBs in a single class. If compiler finds multiple IIBs, then they all are executed from top to bottom i.e. the IIB which is written at top will be executed first.
IIB1 block IIB2 block IIB3 block Constructor Called
Instance Initialization Block with parent class
You can have IIBs in parent class also. Instance initialization block code runs immediately after the call to super() in a constructor. The compiler executes parents class’s IIB before executing current class’s IIBs. Have a look on following example.
B-IIB block B-Constructor Called A-IIB block A-Constructor Called
In above example, compiler tries to execute constructor of class A, when object of class A is created. But it finds super() statement and goes to the parent class constructor first to be executed. Order of execution in this case will be as follows:
I. Instance Initialization Block of super class
II. Constructors of super class
III. Instance Initialization Blocks of the class
IV. Constructors of the class
- Instance Initialization Blocks run every time a new instance is created.
- Initialization Blocks run in the order they appear in the program
- The Instance Initialization Block is invoked after the parent class constructor is invoked (i.e. after super() constructor call)
Related Article :
The Initializer Block in Java
This article is contributed by Vishal Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Initialization of local variable in a conditional block in Java
- Double Brace Initialization in Java
- Order of execution of Initialization blocks and Constructors in Java
- Initialize a static Map in Java using Double Brace Initialization
- Instance Variable Hiding in Java
- Static methods vs Instance methods in Java
- Instance variable as final in Java
- The Initializer Block in Java
- Method and Block Synchronization in Java
- Static Block and main() method in Java
- Constructor Overloading with Static Block in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1