By WebNest Studio Engineering Team
Java - Core Tutorial
Instance Initializer Block
An instance initializer block (IIB) is a block of code enclosed in plain braces { ... } directly inside a class body, without any keyword like static in front of it. Unlike a static block, which runs once when the class is loaded, an instance initializer block runs every single time an object of the class is created — specifically, every time before the constructor body executes.
The syntax is just a pair of braces at class level: { System.out.println("object created"); }. It has no name, no return type and no parameters, and you never call it yourself. Java runs it as part of new.
Why it matters
Instance initializer blocks are rare in everyday code, but the order in which Java runs static blocks, instance blocks and constructors is a classic "what does this print?" question. Knowing the order also explains surprising behaviour during object creation, such as a field being read before the constructor has set it.
Watch: Instance Initializer Block in Java – Complete Guide with Examples | Core Java Tutorial
What an instance initializer block is and exactly when it runs: before the constructor body on every new object, how the compiler copies it into each constructor after super(), multiple blocks and field initializers in textual order, this() chaining, parent-to-child order with static blocks, real uses such as anonymous classes, and tricky output questions.
When Instance Initializer Blocks Run
Java performs object construction in a strict order: first, if this class has a superclass, the superclass's constructor chain runs completely (via the implicit or explicit super(...) call). Then, within this class, instance variable initializers and instance initializer blocks run in the exact top-to-bottom order they appear in the source file. Only after all of that completes does the remaining code in this class's constructor body run.
- Superclass constructor (always runs first, even if implicit).
- Instance variable initializers and instance initializer blocks, in source order.
- The rest of the current constructor's body.
How the Compiler Copies the Block into Every Constructor
An instance block is not a separate method that the JVM calls. When javac compiles the class, it copies the code of every field initializer and instance block into each constructor, placing it immediately after the super(...) call and before the constructor's own statements. That is why the block always runs after the parent is built but before your constructor body, and why it runs no matter which constructor was used. If a class has no constructor, the block goes into the default constructor the compiler generates.
Multiple Blocks and Field Initializers: Textual Order Matters
A class may contain any number of instance blocks. They run from top to bottom, interleaved with field initializers (int x = 10;) in exactly the order they are written. So a block can see a field assigned above it, and a field initializer below a block sees whatever the block set. The reverse is restricted: a block that reads a field declared further down, by its simple name, fails to compile with illegal forward reference. Keep fields at the top and blocks after them, and the order stays easy to follow.
Instance Blocks and this() Constructor Chaining
The copied block code goes only into constructors that call super(...), either explicitly or implicitly. A constructor that starts with this(...) does not get a copy, because it hands off to another constructor of the same class, and that one will run the block. The result is that the block runs exactly once per object, even when constructors chain into each other.
Execution Order with Inheritance
With a parent and a child class, Java first initialises the classes and then builds the object from the top of the hierarchy down.
- Parent static blocks, then child static blocks — once, the first time the class is used.
- For every object: the parent's field initializers and instance blocks, then the parent's constructor body.
- Then the child's field initializers and instance blocks, then the child's constructor body.
- Creating a second object repeats only the per-object steps; static blocks do not run again.
Why Use an Instance Initializer Block
Instance initializer blocks are most useful when a class has multiple overloaded constructors that all need to share some common initialization logic that is too involved for a simple field initializer expression — placing that shared logic in an IIB guarantees it runs no matter which constructor is used, without duplicating code in every constructor. In modern Java, this pattern is relatively uncommon since constructor chaining with this(...) usually achieves the same goal more explicitly, but it still appears in anonymous class initialization and in legacy codebases.
Instance Block vs Static Block
A static block runs exactly once per class, at class-loading time, before any object exists. An instance initializer block runs once per object creation, every time new is used, right before the constructor body — meaning if you create five objects, the instance block runs five times, but a static block in the same class would have already run only once, earlier, at class load.
- Syntax:
static { ... }vs plain{ ... }. - Runs: once per class vs once per object.
- When: during class initialisation vs during
new, aftersuper(...)and before the constructor body. - Can use: only static members vs both static and instance members, including
this. - Typical job: set up static data such as a lookup table vs common setup for every object.
- Full order for one new object of a class used for the first time: static block, then instance block, then constructor.
Rules and Limits
- An instance block may throw a checked exception only if every constructor of the class declares it in its
throwsclause. A block that can never finish normally (it always throws) is rejected withinitializer must be able to complete normally. - It cannot contain a
returnstatement; javac reportsreturn outside method. - It can use
this, instance fields and instance methods, because the object already exists when it runs. - Code in it cannot take arguments, so anything that depends on constructor parameters must stay in the constructor.
Examples
Execution order: static block once, instance block and constructor per object
public class InitDemo {
static {
System.out.println("Static block (runs once, at class load)");
}
{
System.out.println("Instance initializer block (runs before each constructor)");
}
InitDemo() {
System.out.println("Constructor body");
}
public static void main(String[] args) {
System.out.println("Creating first object:");
new InitDemo();
System.out.println("Creating second object:");
new InitDemo();
}
}
Static block (runs once, at class load)
Creating first object:
Instance initializer block (runs before each constructor)
Constructor body
Creating second object:
Instance initializer block (runs before each constructor)
Constructor body
Multiple blocks and field initializers run in the order they are written
public class OrderDemo {
int x = 10; // 1. field initializer
{
System.out.println("Block 1: x = " + x);
x = 20; // 2. first instance block
}
int y = x + 1; // 3. sees x = 20
{
System.out.println("Block 2: y = " + y); // 4. second instance block
}
OrderDemo() {
System.out.println("Constructor: x = " + x + ", y = " + y);
}
public static void main(String[] args) {
new OrderDemo();
}
}
Block 1: x = 10
Block 2: y = 21
Constructor: x = 20, y = 21
With this() chaining the block still runs once per object
public class ChainDemo {
{
System.out.println("instance block");
}
ChainDemo() {
this("guest"); // delegates: the block is NOT copied here
System.out.println("no-arg constructor");
}
ChainDemo(String user) { // calls super() implicitly: the block runs here
System.out.println("constructor for " + user);
}
public static void main(String[] args) {
new ChainDemo();
System.out.println("---");
new ChainDemo("admin");
}
}
instance block
constructor for guest
no-arg constructor
---
instance block
constructor for admin
Full execution order with inheritance (a favourite interview question)
public class InheritDemo {
public static void main(String[] args) {
new Child();
System.out.println("---");
new Child();
}
}
class Parent {
static { System.out.println("1. Parent static block"); }
{ System.out.println("3. Parent instance block"); }
Parent() { System.out.println("4. Parent constructor"); }
}
class Child extends Parent {
static { System.out.println("2. Child static block"); }
{ System.out.println("5. Child instance block"); }
Child() { System.out.println("6. Child constructor"); }
}
1. Parent static block
2. Child static block
3. Parent instance block
4. Parent constructor
5. Child instance block
6. Child constructor
---
3. Parent instance block
4. Parent constructor
5. Child instance block
6. Child constructor
Real use: setting up an anonymous class, which cannot have a constructor
import java.util.ArrayList;
import java.util.List;
public class AnonDemo {
public static void main(String[] args) {
// An anonymous class cannot declare a constructor, so setup goes in an instance block
List<String> langs = new ArrayList<>() {
{
add("Java");
add("Python");
}
};
System.out.println(langs);
}
}
[Java, Python]
Common Mistakes
- Confusing an instance initializer block with a static block just because both use braces — omitting "static" changes it to run per-object instead of once per class.
- Assuming the instance initializer block runs after the constructor body — it always runs before the remaining constructor code, right after the superclass constructor completes.
- Not realizing multiple instance initializer blocks and field initializers all run in strict top-to-bottom source order, which matters if one depends on a value set by another.
- Overusing instance initializer blocks instead of a private helper method or this(...) chaining, making initialization logic harder to follow than necessary.
- Expecting the block to run twice when one constructor calls another with this(...). The block is copied only into the constructor that calls super(...), so it runs once.
- Reading a field in a block placed above that field's declaration, which fails with "illegal forward reference".
Exercise
Add a static block, an instance initializer block and a constructor to Session, each printing one word, so that creating two objects produces the output below.
Expected output: static, instance, constructor, instance, constructor, each on its own line.
class Session {
// TODO: a static block that prints "static"
// TODO: an instance initializer block that prints "instance"
// TODO: a constructor that prints "constructor"
}
public class SessionDemo {
public static void main(String[] args) {
new Session();
new Session();
}
}
Show hints
- A static block is written
static { ... }; an instance block is just{ ... }inside the class. - The static block runs once, when the class is first used. The instance block runs before the constructor body, every time.
Show solution
class Session {
static {
System.out.println("static");
}
{
System.out.println("instance");
}
Session() {
System.out.println("constructor");
}
}
public class SessionDemo {
public static void main(String[] args) {
new Session();
new Session();
}
}
Quick quiz
1. When does an instance initializer block run?
- A. Each time an object is created, before the constructor body
- B. Once, when the class is loaded
- C. After the constructor finishes
- D. Only when called by name
Show answer
A. The compiler copies instance initializer code into every constructor, after the call to super() and before the rest of the body.
2. You create three objects of a class that has a static block. How many times does the static block run?
- A. Three times
- B. Once
- C. Never
- D. Six times
Show answer
B. A static block runs a single time, during class initialisation, regardless of how many objects are created.
3. A class has a field initializer and an instance block that both assign the same field. Which assignment wins?
- A. Always the field initializer
- B. Always the instance block
- C. Whichever appears later in the source file
- D. It is a compile error
Show answer
C. Field initializers and instance blocks run in the order they appear in the class, so the later one overwrites the earlier one.
Interview questions
In what order do static blocks, instance blocks and constructors run, including a parent class?
Show answer
When the class is first used: the parent's static blocks, then the child's static blocks, once each. Then, for every object created: the parent's instance initializers and instance blocks, the parent's constructor body, the child's instance initializers and instance blocks, and finally the child's constructor body.
Why would you use an instance initializer block instead of a constructor?
Show answer
To share initialisation code between several constructors without repeating it or chaining them, and to initialise anonymous classes, which cannot declare a constructor. In ordinary classes, constructor chaining with this(...) or a private helper method is usually clearer, which is why instance blocks are uncommon.
Where does the instance block code actually end up after compilation?
Show answer
javac copies field initializers and instance blocks, in source order, into every constructor that calls super(...), right after that call and before the constructor's own statements. A constructor that begins with this(...) gets no copy, because the constructor it delegates to already contains the code. That is why the block runs exactly once per object.
Can an instance initializer block throw an exception?
Show answer
It can throw unchecked exceptions freely. It can throw a checked exception only if every constructor of the class declares that exception in its throws clause. It also must be able to complete normally, and it cannot contain a return statement.
Key Points to Remember
- An instance initializer block is a brace-only block (no "static") that runs once per object, before the constructor body.
- The compiler copies it into every constructor right after super(...); constructors that start with this(...) do not get a copy.
- Execution order is: superclass constructor, then instance variable initializers and instance initializer blocks in source order, then the current constructor's remaining body.
- With inheritance: parent static, child static (once), then parent instance blocks and constructor, then child instance blocks and constructor (per object).
- A static block runs once per class load; an instance initializer block runs once per object creation.
- Its main real-world use is initialising anonymous classes, which cannot declare constructors.
Go deeper
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering