Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

When exactly is a class initialized in the JVM, and why can a static final constant be read without triggering initialization?

A class is initialised on its first active use: creating an instance, calling a static method, assigning a static field or reading one that is not a constant, certain reflective calls such as Class.forName, or initialising a subclass. Reading a static final constant does not trigger it, because a compile-time constant, a static final primitive or String with a constant initialiser, is inlined by javac into the reading class, so the field is never actually accessed. That inlining means changing a constant requires recompiling its callers. Initialisation runs once under a lock, so circular static dependencies can show half-initialised state or deadlock across threads, and an exception in a static initialiser gives ExceptionInInitializerError and then NoClassDefFoundError on later use.

Why this question is asked

Class initialisation is invisible most of the time, which is why its edge cases are so surprising: a constant change that does not take effect after a deploy, a startup that hangs with two threads stuck in static blocks, a NoClassDefFoundError for a class that is clearly on the classpath. Interviewers ask this internals question to see whether we understand the JVM's rules well enough to diagnose such problems. In this answer, we will recap the difference between loading and initialising, list what triggers initialisation, see why constants do not, look at the binary compatibility trap, examine the lock with its deadlock and error cases, and use these rules to explain the holder-class idiom for lazy singletons.

1A Quick Recap: Loading, Linking and Initialising

A class goes through several steps before it can be used. Loading reads the class file and creates the Class object. Linking verifies the bytecode and prepares static fields with default values, zero or null. Initialising runs the class's static initialiser code: the static field initialisers and static blocks, in textual order, compiled into one special method called <clinit>.

Loading can happen early, for example when another class refers to it, but initialisation is lazy and strictly defined by the Java Language Specification. Until a class is initialised, its static blocks have not run and its static fields are still at their defaults.

2What Triggers Initialisation

A class is initialised just before its first active use. The specification lists these: creating an instance with new, calling a static method declared in the class, assigning a static field declared in it, reading a static field that is not a constant variable, calling certain reflective methods such as Class.forName(name) or reflective access to static members, and initialising a subclass, which first initialises its superclass. The class with the main method is also initialised at startup.

Java
class Config {
static { System.out.println("Config initialised"); }
static final int MAX = 10; // compile-time constant
static final Integer BOXED = 10; // not a constant: Integer is not primitive or String
static int counter = 0; // not final
static void touch() { }
}
System.out.println(Config.MAX); // 10 (nothing else printed: MAX was inlined)
System.out.println(Config.counter); // Config initialised, then 0
System.out.println(Config.BOXED); // 10 (already initialised, so no message)
Config[] arr = new Config[3]; // does not initialise: creating an array is not use of the class
Class<?> c = Config.class; // does not initialise either
Does this initialise class Config?
ActionInitialises?
new Config()Yes
Config.touch()Yes
read Config.counter (not final)Yes
read Config.MAX (static final int = 10)No: inlined constant
read Config.BOXED (static final Integer)Yes: not a compile-time constant
new Config[3] or Config.classNo
Class.forName("Config")Yes
initialising a subclass of ConfigYes, superclass first

Notice the last two lines. Declaring an array of Config or taking Config.class loads the class but does not initialise it. Class.forName("Config") with the single-argument form would initialise it, while the three-argument form with initialize set to false would not.

3Why Constants Do Not Trigger It

A constant variable is a final field of primitive type or String, initialised with a compile-time constant expression, such as static final int MAX = 10 or static final String NAME = "app". javac replaces every read of such a field with the value itself. So System.out.println(Config.MAX) compiles to code that prints the literal 10; the class file of the caller contains no reference to Config.MAX at all. With no field access at run time, there is nothing to trigger initialisation.

This inlining is what makes constants usable in switch labels and annotations, which require values known at compile time. But it only applies to true constants. static final Integer BOXED = 10, or static final int NOW = computeNow(), is a final field whose value is computed at initialisation, so reading it is an active use.

4The Binary Compatibility Trap

Because constants are copied into callers at compile time, changing a constant in a library does not change callers that were compiled earlier. If library version 1 had static final int MAX = 10 and version 2 changes it to 20, a service compiled against version 1 still uses 10 after we deploy version 2, until the service itself is recompiled. Nothing fails; the old value just quietly remains.

So public constants should be values that will truly never change, such as a protocol version that is fixed by a specification. For values that may change, we expose a static method, such as static int maxRetries(), or a field initialised in a way that is not a constant expression, so callers read the current value at run time.

5Initialisation Runs Once, Under a Lock

The JVM guarantees that a class is initialised exactly once, even if many threads use it at the same moment. The first thread takes an initialisation lock for the class and runs <clinit>; other threads that need the class wait until it finishes, and then see all of its static state safely published. This guarantee is useful, but it has two sharp edges.

The first is circular dependencies within one thread. If A's static initialiser reads a static field of B, and B's static initialiser reads a static field of A, the thread initialising A starts B, B asks for A, and the JVM sees that the same thread is already initialising A, so it lets B continue with A half-initialised. B then reads A's fields at their default values, typically null or 0.

The second is the same cycle across two threads. If thread 1 starts initialising A while thread 2 starts initialising B, each holds one class's initialisation lock and waits for the other. Both threads hang forever, and a thread dump shows them waiting in <clinit>, the static initialiser method.

Step throughTwo threads deadlock on class initialisation
Thread 1Class AClass BThread 2
first use of A: lock A, run A's static block
first use of B: lock B, run B's static block
A's static block reads B.x: wait for B
B's static block reads A.y: wait for A
blocked forever
blocked forever

6 messages in order. Play them, or step through them one at a time.

Each thread holds one class's initialisation lock and waits for the other's.

6When a Static Initialiser Throws

If a static initialiser throws an exception, initialisation fails and the class is marked as erroneous. The first use throws ExceptionInInitializerError, which wraps the original exception. Every later attempt to use the class throws NoClassDefFoundError with a message like Could not initialize class Config, and the JVM does not retry.

Java
class Settings {
static final int PORT = Integer.parseInt(System.getenv("PORT")); // NumberFormatException if PORT is unset
}
try { int p = Settings.PORT; } catch (Throwable t) { System.out.println(t); }
// java.lang.ExceptionInInitializerError (cause: NumberFormatException)
try { int p = Settings.PORT; } catch (Throwable t) { System.out.println(t); }
// java.lang.NoClassDefFoundError: Could not initialize class Settings

This is why a NoClassDefFoundError in production logs should send us looking for the first ExceptionInInitializerError earlier in the same log, which contains the real cause. The class file is present; it simply failed to initialise once and is now unusable for the life of its class loader.

7Follow-up questions

How does the holder-class idiom use these rules for lazy singletons?

We put the singleton instance in a private static nested class: class Service { private static class Holder { static final Service INSTANCE = new Service(); } static Service get() { return Holder.INSTANCE; } }. Holder is not initialised when Service is, because nothing uses it yet. The first call to get() reads Holder.INSTANCE, which is not a compile-time constant, so it triggers Holder's initialisation. The JVM runs it once, under its initialisation lock, and publishes the result safely to all threads. So we get lazy creation and thread safety with no synchronized or volatile in our code, and later calls cost only a field read.

Are interfaces initialised the same way?

Mostly, but initialising a class does not initialise the interfaces it implements, unless an interface declares default methods, in which case it is initialised when an implementing class is. Reading a non-constant field of an interface initialises that interface.

How do you find a class initialisation deadlock?

Take a thread dump with jstack or jcmd Thread.print. Threads stuck in class initialisation are shown waiting while running a static initialiser, which appears as <clinit> in the stack. The fix is to break the cycle between the static initialisers, for example by moving shared constants into a third class with no dependencies or making one of the values lazy.

8Conclusion

In this answer, we saw that a class is initialised just before its first active use: new, a static method call, a static field write or a read of a non-constant static field, certain reflective calls, or initialising a subclass. Compile-time constants are inlined by javac, so reading them does not touch the class, and changing them needs recompiled callers. Initialisation runs once under a lock, which makes circular static dependencies risky, either half-initialised values in one thread or a deadlock across two, and a failing static initialiser causes ExceptionInInitializerError followed by NoClassDefFoundError. The holder-class idiom builds on these same rules for lazy, thread-safe singletons.

What to say in the interviewAbout 80 seconds aloud

Open with

A class is initialised lazily, just before its first active use. That means creating an instance, calling a static method, writing a static field, reading a static field that isn't a compile-time constant, certain reflection like Class.forName, or initialising a subclass, which initialises the superclass first.

Then, if they want more

Just declaring a variable or an array of the type, or taking Foo.class, doesn't do it. A static final primitive or String with a constant initialiser is different: javac copies the value into the calling class at compile time, so at run time there's no field access and nothing to trigger initialisation.

The flip side is a binary compatibility trap: if a library changes such a constant, callers compiled earlier keep the old value until they're recompiled, so I only make truly fixed values public constants.

Initialisation runs exactly once under a per-class lock, which gives safe publication, but circular static dependencies are dangerous: in one thread you can read the other class's fields while they're still null, and across two threads you can deadlock in the static initialisers.

And if a static initialiser throws, the first use gives ExceptionInInitializerError, and every later use gives NoClassDefFoundError, could not initialize class. These same rules make the holder-class idiom work for lazy, thread-safe singletons.

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 "When exactly is a class initialized in the JVM, and why can a static final constant be...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/be832346f9/