Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java static final vs final: Key Differences

Understand how static final and final differ in Java: where values live, how assignment works, compile-time inlining, memory footprint, and concurrency guarantees.

~5 min readJava
#Java#static final#final keyword#Java constants
Diagram contrasting a static final class constant shared across instances with a final instance field holding a separate value per object.

In Java, static final and final answer different questions. static controls whether a field belongs to the class itself or to each instance; final controls whether the field can be reassigned after initialization. Combining them creates a class-level constant, while final alone creates a per-instance value assigned once. The two declarations also behave differently at compile time, in memory use, and under concurrency.

What final Means on an Instance Field

A final instance field must be assigned exactly once during object initialization. If it is blank, the value must be assigned in an instance initializer block or in every constructor path. If it has an initializer at the declaration site, that assignment happens before the constructor body runs.

java
public class Order { private final String orderId; public Order(String orderId) { this.orderId = orderId; } }

Each Order instance holds a different orderId, but that value cannot change for the lifetime of the object; the compiler rejects any attempt to reassign it.

What static final Adds

Adding static moves the field from the instance to the class. There is one copy shared by all instances, initialized once when the class is first loaded and initialized.

java
public class Config { public static final int MAX_RETRIES = 3; }

MAX_RETRIES is a single value associated with Config, not with any particular instance. Every reference to Config.MAX_RETRIES reads the same value, and no instance of Config is required to access it. This is the standard way to declare a constant in Java.

Compile-Time Constants and Inlining

When a static final field holds a primitive or a String and is initialized with a constant expression, the Java compiler treats it as a compile-time constant. References to the field are replaced with the literal value during compilation.

java
public static final double PI = 3.14159;

Code that reads PI is compiled as if it read 3.14159 directly. This inlining has a practical consequence: if the value changes and the referencing classes are not recompiled, they keep the old literal. In a multi-module build, changing a public static final constant requires recompiling every module that references it, or those modules will continue using the stale value.

Constructor-assigned final instance fields are not compile-time constants; the compiler cannot inline a value that is not known until an object is created. A final instance field initialized at its declaration with a constant expression is the exception and can be treated as a constant variable, but the common per-instance final field is assigned from constructor input.

Runtime and Memory Behavior

The memory footprint differs significantly. A static final field exists once per class loader. A final instance field exists once per object. If you create 10,000 Order objects, you get 10,000 copies of orderId. If you reference a static final constant, you get one copy regardless of how many instances exist.

This matters when a field would be identical across all instances. Using static final avoids duplicating the same value in every object. For a small primitive this saving is negligible, but for a large immutable object or a frequently allocated class, the difference adds up.

A static final reference to a mutable object is not a constant value; it is a constant reference. The object it points to can still be mutated.

java
public static final List<String> DEFAULTS = new ArrayList<>();

DEFAULTS cannot be reassigned, but the list can be modified by any code that reaches it. If you intend a true constant, prefer an unmodifiable collection or a primitive/String value.

Concurrency and Visibility

static final fields are initialized during class initialization, which the JVM guarantees is visible to all threads that use the class. The class initialization process includes synchronization that safely publishes the static state. No additional synchronization is needed to read a static final field after the class has been initialized.

final instance fields have a different guarantee. After the constructor completes, any thread that reads the object's final fields sees the values assigned in the constructor, provided the object reference was published safely. This is the final field semantics defined by the Java Memory Model.

For immutable data holders, final instance fields give you thread-safe publication without synchronization. If an object's state is entirely final and never mutated after construction, it can be shared across threads without locks, as long as the reference itself is published through a safe mechanism such as a concurrent collection or a volatile field.

Choosing Between static final and final

Use static final when the value is the same for every instance and is known at compile time or loaded once for the class. Typical cases include configuration constants, mathematical constants, and shared immutable defaults that should not be duplicated per object.

Use final when the value is fixed per instance but varies between instances. Typical cases include an object's identity, an ID assigned at construction, or a reference to a collaborator passed into the constructor.

java
public class HttpClient { private static final int CONNECT_TIMEOUT_MS = 5000; private final String baseUrl; public HttpClient(String baseUrl) { this.baseUrl = baseUrl; } }

Here CONNECT_TIMEOUT_MS is shared by every HttpClient, while baseUrl is specific to each instance. Mixing the two modifiers in one class is common and correct when the fields have different scopes.

Common Mistakes and Edge Cases

A blank static final field must be assigned in a static initializer. A blank final instance field must be assigned in an instance initializer or on every constructor path.

java
public class Service { private static final String NAME; static { NAME = loadName(); } }

If a blank final field is not assigned on every constructor path, the compiler rejects the class. The same compile-time enforcement covers blank static final fields in static initializers. This is a compile-time error, not a runtime failure, so the JVM never sees a partially initialized final field from normal code.

Another edge case involves reflection. In some JVM versions, reflection can bypass final on instance fields and modify them after construction. This is not behavior you should rely on; the language guarantees apply to normal compiled code, and reflective mutation of final fields is explicitly discouraged and may fail or behave unpredictably depending on the JVM and module system.

A related subtlety: a static final field that references an immutable object, such as a String, is safe to share widely. A static final field that references a mutable object requires the same defensive thinking as any shared mutable state. The modifier prevents reassignment of the reference, not mutation of the object. When the field must be a true constant, keep the referenced object immutable or unmodifiable.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.