skip to content

In Java, how does initializing a static final field differ from an instance final field, and what is a compile-time constant?

level: seniorimportance: should knowfreq 45%

answer

  1. static final = one shared value, class-init time
  2. Set at declaration or in a static block, never a constructor
  3. Primitive/String + constant expression = compile-time constant → inlined
  4. Inlining = recompile consumers when the constant changes
  5. Break inlining with a non-constant initializer

basics

~20 s

A static final field is set once at class initialization, at its declaration or in a static block, never in a constructor. A compile-time constant is a final primitive or String with a constant-expression initializer, copied into using code.

solid answer

~50 s

A `static final` field is a class-level field assigned exactly once when the class is initialized, not per object. You set it at its declaration or in a static initializer block, never in a constructor. A final variable of primitive or `String` type initialized with a constant expression is a **constant variable**, the JLS name for a compile-time constant; it need not be `static`. A reference to such a field must be resolved at compile time, so its value is copied into the code that reads it. If you change the constant and recompile only its class, callers compiled earlier keep the old value until they are recompiled. Reading one also does not trigger class initialization. A `static final` that is not a constant (e.g. initialized from a method call) is computed during class initialization and read at run time. Instance final fields can vary per object and are assigned during construction.

go deeper

for a junior

Knows a static final field is one shared value, set once for the class.

for a middle

Distinguishes static final (class, class initialization) from instance final (per object, constructor) and knows static finals aren't set in constructors.

for a senior

Explains compile-time constants, inlining, and the recompile-the-consumers versioning hazard, plus blank static finals via static blocks.

for a principal

Weighs API design implications of public constants (inlining/binary-compatibility), chooses constant vs. accessor, and reasons about class-initialization ordering and lazy init.

## Static vs instance - An **instance field** belongs to each object; each `new` creates its own copy. - A **static field** belongs to the **class itself**: there is one shared copy regardless of how many objects exist. Adding `final` to either makes it assign-once. But *when* and *where* differ: | | instance final | static final | |---|---|---| | Belongs to | each object | the class | | Assigned when | during construction | at class initialization (once) | | Assigned where | declaration, instance block, or constructor | declaration or **static initializer block** | | Per-object value? | yes | no, shared | A `static final` field is **never** assigned in a constructor or an instance block, because those run per object and would violate exactly-once across many instances. The compiler rejects it. ## Where you assign a static final Two places: ```java static final int A = 10; // at declaration static final int B; static { B = compute(); } // in a static initializer block ``` A blank `static final` (declared without a value) must be definitely assigned by the **static initializer**(s), analogous to a blank instance final being assigned by constructors. ## Class initialization timing Static field initializers and static blocks run **once**, in source order, when the class is **initialized** (JLS 12.4.1): immediately before the first instance creation, static method call, static field assignment, or use of a static field that is not a constant variable. Initialization is distinct from loading: `Class.forName(name, false, loader)` loads a class without running its static blocks. ## Compile-time constants (constant expressions) A special, important case: a variable that is - `final` (`static` is not required: an instance field or a local variable qualifies too), **and** - of a **primitive type or `String`**, **and** - initialized with a **constant expression** (literals and operations on other compile-time constants), is what the JLS calls a **constant variable**, and interviews call a compile-time constant. A boxed type does not qualify: `static final Integer B = 1;` is not one, and neither is a blank final. The JLS **requires** a reference to such a field to be resolved at compile time to its value, and for a static one no reference to the field should be present in the bytecode. ```java public static final int MAX = 100; // compile-time constant, inlined at use sites ``` Because the value is copied, reading such a constant does not initialize the class that declares it: its static blocks do not run. ### The inlining hazard Because the value is copied into *consumers* at their compile time, changing the constant and recompiling **only** the defining class does **not** update consumers that were compiled earlier: they keep the old literal and raise no error. You must recompile the consumers too. This is a classic versioning gotcha for public constants in libraries. To avoid inlining (so the value is read at runtime), make it **not** a constant expression, e.g.: ```java public static final int MAX = Integer.valueOf(100); // not a constant expression → read at runtime ``` or compute it in a static block. ## Compile-time constants enable other features Because they are known at compile time, compile-time constants can be used as `case` labels in a `switch`, as annotation element values, and they participate in conditional-compilation-style dead-code elimination (`if (false)` with a constant). Non-constant `static final` fields cannot. ## Summary Static final = one shared, assign-once value set at class initialization (declaration or static block), never in a constructor. Any final primitive or `String` initialized with a constant expression is a compile-time constant and gets inlined into consumers, which is fast but creates a recompile-the-consumers versioning hazard. Instance finals differ: they are per-object and assigned during construction.

  • Why might a library bump a constant's value but callers still see the old one?
    Compile-time constants (final primitive or String variables initialized with a constant expression) are inlined into the callers' bytecode at their compile time. Recompiling only the library does not refresh callers; they must be recompiled to pick up the new literal.
  • How do you make a static final field NOT a compile-time constant?
    Initialize it with something that isn't a constant expression — e.g. a method call, Integer.valueOf(100), or assignment in a static block. Then it is read at runtime instead of being inlined.
  • Does reading a compile-time constant from another class initialize that class?
    No. JLS 12.4.1 lists use of a static field as a trigger only when it is not a constant variable, because the value was copied at compile time. Reading a non-constant static final does initialize the class and runs its static blocks.

  • Trying to assign a static final field in a constructor
  • Assuming every static final is inlined (only primitive/String constant expressions are)
  • Thinking changing a public constant updates all callers without recompiling them
  • Confusing class-init timing (once) with per-object construction
  • Believing a compile-time constant has to be static
  • Assuming any read of a static final triggers class initialization

context