skip to content

In Java, what is the difference between static initialization and instance initialization, and when does each one run?

level: juniorimportance: must knowfreq 70%

answer

  1. static = class, once at load
  2. instance = object, every new
  3. static block can't see instance fields
  4. instance code can read static fields
  5. textual order within each phase

basics

~20 s

Static initializers (static fields and static blocks) run once, when the class is first loaded. Instance initializers (instance fields and instance blocks) run every time you create an object with new, just before the constructor body.

solid answer

~40 s

Java has two distinct initialization phases. Static initialization covers static field assignments and static { } blocks; it runs exactly once, when the class is first used (loaded and initialized by the JVM), and the members belong to the class itself. Instance initialization covers instance field assignments and instance { } blocks; it runs once per object, every time new is called, after the super constructor returns and before the body of the current constructor executes. Within each phase, members run in the textual order they appear in the source file. So static setup is one-time per class, while instance setup repeats for each object. A common follow-up is that a static block can never see instance fields, but instance code can read static fields because the class is already initialized by then.

go deeper

for a junior

Can state that static runs once at class load and instance runs on every new, before the constructor body.

for a middle

Explains the lazy class-loading trigger and that members run in textual order within each phase.

for a senior

Distinguishes class initialization from class loading/linking and knows compile-time constants don't trigger init.

for a principal

Discusses implications for singletons, lazy holders, and initialization deadlocks under concurrent class loading.

## What "initialization" means When you declare a field with a value, e.g. `int x = 5;`, that assignment is *initialization code*. Java also lets you write standalone blocks of code that run during initialization. There are two completely separate kinds, distinguished by the keyword `static`. ## Static members vs instance members - A **static** field or block belongs to the **class** itself — there is one copy, shared by everyone, no matter how many objects exist (or even if none do). - An **instance** (non-static) field or block belongs to **each object** — every `new` creates a fresh, independent copy. ```java class Counter { static int created = 0; // ONE shared copy int id; // one per object } ``` ## When static initialization runs The JVM loads a class lazily — only when it is *first actively used* (first `new`, first access to a static member, etc.). At that moment, exactly once for the whole program, the JVM runs the class's **static initialization**: all static field initializers and all `static { ... }` blocks, in the order they appear in the source. This never repeats. ```java class Config { static int n = compute(); // runs once static { System.out.println("loaded"); } // runs once } ``` ## When instance initialization runs Every time you write `new SomeClass(...)`, the JVM performs **instance initialization** for that one object: all instance field initializers and all `{ ... }` (non-static, "instance") blocks run, in source order, and then the constructor body runs. This repeats for every object you create. ```java class Box { int v = 10; // runs every new { System.out.println("made a box"); } // instance block, runs every new Box() { /* body runs last */ } } ``` ## The key ordering fact at this level By the time any instance code runs, the class has already been loaded and statically initialized. That is why an instance block can freely read a static field, but a static block cannot read an instance field (no object exists yet during static init). This ordering — static once, then instance per-object — is the foundation for the more detailed cross-hierarchy rules. ## Summary table | | Belongs to | Runs | How often | |---|---|---|---| | static field / `static {}` | class | first use of class | once | | instance field / `{}` | object | each `new` | every object |

  • Can a static initializer block read an instance field?
    No. Static init runs at class load when no object exists, so there is no instance field to read; the compiler rejects it.
  • What triggers static initialization of a class?
    The first active use: creating an instance, accessing a static field (non-constant), calling a static method, or reflective initialization.

saying these in an interview costs you the question

  • Thinking static blocks run on every new (they run once)
  • Thinking instance fields are initialized at class load
  • Believing a static block can reference this or an instance field

context