The Initializer Block in Java Last Updated : 12 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initializer block contains the code that is always executed whenever an instance is created and it runs each time when an object of the class is created. There are 3 areas where we can use the initializer blocks: ConstructorsMethodsBlocks Tip: If we want to execute some code once for all objects of a class then we will be using Static Block in Java Example 1: Java // Java Program to Illustrate Initializer Block // Class class Car { // Class member variable int speed; // Constructor Car() { // Print statement when constructor is called System.out.println("Speed of Car: " + speed); } // Block { speed = 60; } // Class member method public static void main(String[] args) { Car obj = new Car(); } } OutputSpeed of Car: 60 Example 2: Java // Java Program to Illustrate Initializer Block // Importing required classes import java.io.*; // Main class public class GFG { // Initializer block starts.. { // This code is executed before every constructor. System.out.println( "Common part of constructors invoked !!"); } // Initializer block ends // Constructor 1 // Default constructor public GFG() { // Print statement System.out.println("Default Constructor invoked"); } // Constructor 2 // Parameterized constructor public GFG(int x) { // Print statement System.out.println( "Parameterized constructor invoked"); } // Main driver method public static void main(String arr[]) { // Creating variables of class type GFG obj1, obj2; // Now these variables are // made to object and interpreted by compiler obj1 = new GFG(); obj2 = new GFG(0); } } OutputCommon part of constructors invoked !! Default Constructor invoked Common part of constructors invoked !! Parameterized constructor invoked Note: The contents of the initializer block are executed whenever any constructor is invoked (before the constructor's contents)The order of initialization constructors and initializer block doesn't matter, the initializer block is always executed before the constructor. For more details about the Instance Initialization in Java, refer to the article Instance Initialization Block (IIB) in Java Comment More infoAdvertise with us Next Article Order of Execution of Initialization Blocks and Constructors in Java K kartik Improve Article Tags : Java Practice Tags : Java Similar Reads Java Constructors In Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall 10 min read Default constructor in Java Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default 1 min read Private Constructors and Singleton Classes in Java In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new insta 2 min read Copy Constructor in Java Like C++, Java also supports a copy constructor. But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. A prerequisite prior to learning copy constructors is to learn about constructors in java to deeper roots. Below is an example Java program that shows a simpl 4 min read Constructor Chaining In Java with Examples Constructor chaining is the process of calling one constructor from another constructor with respect to current object. One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor (by means of constructor overloading) and make code more readable. Prerequ 5 min read Why Constructors are not inherited in Java? Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super clas 2 min read Constructor Overloading in Java Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be do 5 min read Static Blocks in Java In simpler language whenever we use a static keyword and associate it to a block then that block is referred to as a static block. Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the 4 min read The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial 2 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 Like