skip to content

Static Initializer Blocks

Static initializer blocks run once when the class is initialized, in textual order alongside static field initializers. Interviewers use them in ordering puzzles and to discuss lazy class-initialization guarantees.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is a static initializer block in Java, and why would you use one instead of just initializing static fields inline?

level: juniorimportance: must knowfreq 55%

answer

  1. `static { ... }` — class-level setup
  2. Runs once, at class initialization (first active use)
  3. For statement-level setup: loops, try/catch, multi-field
  4. Only touches static state (no this)
  5. Inline init and static block = same phase

basics

~20 s

A static block is code inside static { ... } that runs once when the class is first loaded. You use it to set up static fields when the setup needs more than one line, like a loop, a try/catch, or several steps.

solid answer

~40 s

A static initializer block is a `static { ... }` block placed directly in a class body. The JVM runs it exactly once, when the class is initialized (typically the first time it's used). Its job is class-level (not per-instance) setup of `static` fields. You reach for it instead of inline initialization (`static int x = 5;`) when the setup needs more than a single expression: populating a static collection in a loop, building a value inside a try/catch (e.g. loading a resource or driver), or computing several static fields together with shared intermediate state. Inline initializers and static blocks both run at class init, in textual order, so they're really two syntaxes for the same phase; the block just gives you full statement-level code instead of a single expression.

go deeper

for a junior

Knows the static { } syntax, that it runs once for class-level setup, and the basic use case (populating a static collection, try/catch setup).

for a middle

Distinguishes class loading from initialization, knows initialization is lazy (first active use) and happens once, and can explain when to prefer a block over inline init.

for a senior

Articulates the active-use triggers, the compile-time-constant exception, that inline initializers and blocks share the same phase/order, and the checked-exception restriction.

for a principal

Reasons about failure modes (ExceptionInInitializerError → erroneous class → NoClassDefFoundError), per-classloader semantics, and why heavy/fragile static-block work is an antipattern for startup time and testability.

## What a static initializer block is In Java, a **class** is a template, and you can attach data and behavior either to each *instance* (object) or to the *class itself*. Members marked `static` belong to the class — there is one copy shared by everyone, not one per object. A **static initializer block** is a chunk of code written like this, directly inside the class body: ```java class Config { static int x; static { // <-- this is a static initializer block x = compute(); } } ``` It has no name, no parameters, no return type — just the keyword `static` followed by `{ ... }`. You cannot call it yourself; the JVM calls it for you. ## When does it run? (class loading vs. class initialization) Two distinct things happen to a class: 1. **Loading** — the JVM reads the `.class` bytecode into memory. This can happen early. 2. **Initialization** — the JVM runs the class's static setup: all `static` field initializers and all `static { }` blocks. Static blocks run during *initialization*, not loading. Initialization happens **lazily**, the *first* time the class is *actively used*, and **exactly once** per class loader. "Actively used" includes: creating an instance (`new Config()`), calling a static method, or reading/writing a non-constant static field. (A `static final` *compile-time constant* like `static final int N = 5;` does **not** trigger initialization — its value is inlined by the compiler.) Because it runs once, a static block is the natural home for one-time class-level setup. ## Why use it instead of inline initialization? Inline initialization assigns a single expression: `static List<String> names = List.of("a", "b");`. That works for simple values. A static block is better when setup needs **statements**, not just one expression: - **Building a collection in a loop** — fill a `Map`/`List` with many entries. - **try/catch is required** — e.g. loading a properties file or a JDBC driver can throw a checked exception; you can't put a `try` in an inline initializer. - **Several fields share intermediate work** — compute something once and assign it into multiple static fields. They are not competing mechanisms: inline static initializers and static blocks both execute in the same initialization phase, in the order they appear in the source (covered in the ordering question). The block is simply the full-statement form. ## A concrete example ```java class Lookup { static final Map<String, Integer> ROMAN = new HashMap<>(); static { ROMAN.put("I", 1); ROMAN.put("V", 5); ROMAN.put("X", 10); } } ``` Here the map *reference* is created inline, and the block populates it — something inline syntax can't express in one expression. ## Caveats to remember - It runs **once**, automatically — don't expect to re-run it. - It can only touch **static** state (no `this`, no instance fields). - An exception thrown in a static block becomes an `ExceptionInInitializerError`, and the class is left in a broken ("erroneous") state — later use throws `NoClassDefFoundError`. So keep static-block logic robust.

  • Can a static block throw a checked exception?
    No. A static initializer cannot complete by throwing a checked exception — the compiler forbids it. You must catch checked exceptions inside the block. Unchecked exceptions can escape, and the JVM wraps them in an ExceptionInInitializerError.
  • What triggers a class to be initialized, running its static blocks?
    The first active use: creating an instance, invoking a static method, or accessing a non-constant static field. Compile-time constant static finals do not trigger it because the compiler inlines their value.

saying these in an interview costs you the question

  • Saying a static block runs every time you create an object (it runs once, per class load, not per instance)
  • Claiming it runs at JVM startup or when the class is loaded — it runs at class *initialization* (first active use), which is lazy
  • Thinking you can access `this` or instance fields from a static block
  • Believing a static block can return a value or be called manually

context

open as a page

When a class has multiple static initializer blocks and static field initializers interleaved, in what order do they execute?

level: middleimportance: must knowfreq 60%

basics

~20 s

They run top to bottom, in the exact order they appear in the source code. Static field initializers and static blocks are treated as one combined sequence, and each runs once when the class is initialized.

open as a page

How does static initializer execution relate to instance initializers and constructors across a class hierarchy when you create an object?

level: middleimportance: should knowfreq 40%

basics

~20 s

Static blocks run first and only once (when the class is first loaded). Then for each new object: the parent's instance setup and constructor run, then the child's. Static = once per class; instance initializers/constructors = once per object.

open as a page

What exactly triggers a class's static initializer to run, and what does NOT trigger it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Static blocks run when the class is first actively used: creating an object, calling a static method, or reading/writing a normal static field. Just declaring a variable of the type, or reading a compile-time constant, does NOT trigger it.

open as a page

What happens when a static initializer block throws an exception, and why is heavy logic in static blocks risky?

level: seniorimportance: should knowfreq 35%

basics

~20 s

If a static block throws, the JVM wraps it in an ExceptionInInitializerError and marks the class as broken. Any later attempt to use that class throws NoClassDefFoundError. So a failure in a static block is fatal for that class and can't be retried.

open as a page