Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are static and instance initializer blocks, when would you use each, and what happens if a static block throws an exception?

A static initializer block runs once, when the class is initialized, and is used for static setup that does not fit in one expression, such as filling a lookup table. An instance initializer block runs on every construction, right after the superclass constructor and before the constructor body, because the compiler copies it into each constructor. If a static block throws an unchecked exception, the first caller gets ExceptionInInitializerError and the class is unusable from then on, with NoClassDefFoundError on every later use.

Why this question is asked

Initializer blocks are a small feature that most of us rarely write, but we meet them all the time: in older code that fills static maps, in anonymous classes, and in the infamous double brace initialization. They also explain a class of startup failures that are hard to read in the logs. Interviewers ask this question to check that we know when each block runs and what happens when a static block fails. In this answer, we will look at both kinds of block, see the exact order in which they run, look at the special case of anonymous classes, find out what happens when a static block throws, and finish with the more readable alternatives.

1What the Two Kinds of Block Look Like

An initializer block is a block of code written directly in a class body, outside any method. With the static keyword in front it is a static initializer; without it, it is an instance initializer.

Java
class Currency {
static final Map<String, Integer> DIGITS = new HashMap<>();
static { // static initializer: runs once per class
DIGITS.put("USD", 2);
DIGITS.put("JPY", 0);
DIGITS.put("BHD", 3);
}
final List<String> log = new ArrayList<>();
{ // instance initializer: runs for every new object
log.add("created");
}
}

The static block belongs to the class. It runs as part of class initialization, which happens the first time the class is really used, and the JVM makes sure it runs only once even if several threads arrive together. Its natural job is static setup that needs more than one expression, like filling the DIGITS table above.

The instance block belongs to each object. It runs every time we create a Currency, and it can use this and the instance fields, just like a constructor.

2The Order in Which They Run

The easiest way to remember the order is to print it.

Java
class Parent {
Parent() { System.out.println("Parent constructor"); }
}
class Child extends Parent {
static { System.out.println("Child static block"); }
{ System.out.println("Child instance block"); }
Child() { System.out.println("Child constructor"); }
}
new Child();
// Child static block (once, when Child is first used)
// Parent constructor
// Child instance block
// Child constructor
new Child();
// Parent constructor
// Child instance block
// Child constructor
Step throughWhat happens on new Child()
  1. Is Child initialized yet?static fields and static blocks run once per class
    YesContinueNoRun the static blocks in source order first
  2. Call super()Parent's constructor runs completely
  3. Run instance initializersfield initializers and instance blocks, in source order
  4. Run the constructor bodythe code after super() in Child()

4 steps in order. Play them, or step through them one at a time.

Notice that the static block runs only for the first object. Notice also that the instance block runs after the Parent constructor, not before it. The reason is how javac compiles it: it copies the instance block, together with all instance field initializers in source order, into every constructor, right after the call to super(). A constructor that starts with this(...) does not get a copy, because the constructor it delegates to already has one, so the block still runs exactly once per object.

3Why Anonymous Classes Need Instance Blocks

An anonymous class has no name, so it cannot declare a constructor. If it needs setup code, an instance initializer block is the only place to put it.

Java
Runnable task = new Runnable() {
private final long started;
{ // the only way to run setup code here
started = System.nanoTime();
}
@Override public void run() { System.out.println(System.nanoTime() - started); }
};

The same mechanism is behind double brace initialization, new HashMap<>() {{ put("a", 1); }}. The outer braces create an anonymous subclass of HashMap and the inner braces are its instance block. It looks neat, but it creates a new class for every use, and when written inside an instance method the anonymous class keeps a hidden reference to the enclosing object, which can cause memory leaks. Map.of("a", 1) is the modern replacement.

4What Happens When a Static Block Throws

First, the compiler does not let a checked exception escape a static block. There is no caller to declare it to, so code like reading a file must catch IOException inside the block. An instance block may throw a checked exception only if every constructor declares it.

An unchecked exception can escape, and then the JVM wraps it in ExceptionInInitializerError for the code that triggered initialization. The class is now marked as failed, and the JVM never tries again in that class loader. Every later use throws NoClassDefFoundError.

Java
class Config {
static final int PORT;
static {
PORT = Integer.parseInt(System.getProperty("port")); // NumberFormatException when the property is missing
}
}
try { System.out.println(Config.PORT); }
catch (Throwable t) { System.out.println(t); }
// java.lang.ExceptionInInitializerError
try { System.out.println(Config.PORT); }
catch (Throwable t) { System.out.println(t); }
// java.lang.NoClassDefFoundError: Could not initialize class Config

So the second message hides the real reason. When we see Could not initialize class in a log, we must search for the first ExceptionInInitializerError, whose cause is the NumberFormatException that started it all. Recent JDKs attach that original error as the cause of the later NoClassDefFoundError as well, which helps; check your version.

5More Readable Alternatives

Most static blocks can be replaced by a field initializer that calls a well-named private static method, or by a factory such as Map.of. The intent is visible at the field, and the method can be tested on its own.

Java
class Currency {
static final Map<String, Integer> DIGITS = Map.of("USD", 2, "JPY", 0, "BHD", 3); // immutable too
static final int[] SQUARES = squares(100);
private static int[] squares(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = i * i;
return a;
}
}

Instance blocks are usually better written as a constructor, or as a private method that every constructor calls, because readers look for setup code in constructors first. And anything that can fail because of the environment, such as reading properties, belongs in an explicitly called method or a managed bean, not in a static block.

6Follow-up questions

Can a class have several static blocks?

Yes. They run once each, in the order they appear in the source, interleaved with the static field initializers. That ordering is why a static block can only refer by simple name to static fields declared above it, and why splitting setup across several blocks makes code harder to follow.

Does reading a static final constant trigger the static block?

Not if the field is a compile-time constant, such as static final int MAX = 10. The compiler copies the value into the calling code, so the class is not initialized. Reading a static final field whose value is computed at runtime, or calling a static method, does trigger initialization.

Can I recover from a failed static initializer without restarting?

Not for that class in that class loader. The JVM records the failure and refuses to retry. The only ways out are restarting the JVM or loading the class again in a new class loader, which is why static initializers should never contain code that can fail for temporary reasons.

7Conclusion

In this answer, we saw that a static block runs once when the class is initialized and is meant for static setup such as lookup tables, while an instance block runs for every object, after the superclass constructor and before the constructor body, because javac copies it into each constructor. Anonymous classes use instance blocks because they have no constructors. A static block cannot throw checked exceptions, and an unchecked one gives ExceptionInInitializerError once and NoClassDefFoundError forever after, so we prefer field initializers with helper methods, factories like Map.of, and explicit setup for anything that can fail.

What to say in the interviewAbout 80 seconds aloud

Open with

A static initializer block is code in the class body marked static. It runs once, when the class is initialized on first real use, and the JVM guarantees it runs only once even with many threads.

Then, if they want more

I would use it for static setup that does not fit one expression, like filling a lookup table, though usually a field initializer calling a private static method, or Map.of, reads better.

An instance initializer block has no keyword and runs for every object. javac copies it, along with the instance field initializers, into each constructor right after the super call, so the order is superclass constructor, then instance initializers, then the constructor body.

Its one real use today is anonymous classes, which cannot declare constructors, and that is also how double brace initialization works, which I avoid. If a static block throws, a checked exception is not allowed to escape at all, and an unchecked one is wrapped in ExceptionInInitializerError for the first caller.

The class is then marked as failed, and every later use in that class loader gets NoClassDefFoundError saying it could not initialize the class, so the real cause is only in that first error. That is why I keep anything that can fail, like reading configuration, out of static blocks.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are static and instance initializer blocks, when would you use each, and what...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/366d697b4b/