Private static final vs private final in Java
Codemia Knowledge Hub ·
A private static final field in Java belongs to the class and is shared by every instance. A private final field belongs to each object and can hold a different value per instance. static is the only difference in the declaration, but it changes where the value lives, when it is initialized and what the code says about intent.
Picking the wrong one rarely causes a compiler error. It causes wasted memory, confusing APIs, or bugs when a value that should be constant for the class varies per instance.
What each keyword does
| Modifier | Effect |
private | Restricts access to the declaring class only |
final | Prevents reassignment after initialization |
static | Binds the field to the class, not to any instance |
With static, a final field becomes a compile-time or class-load-time constant. Without static, it is an immutable property of each object.
The two side by side
MAX_RETRIES exists once in the JVM, and every Connection reads the same value. host is created for every new Connection(...) call and holds whatever string the constructor received.
When each must be assigned
A static final field not assigned by the end of the static initializer is a compilation error. A final instance field not assigned by the end of every constructor is also a compilation error.
Memory and lifecycle
| Aspect | private static final | private final |
| Storage | One copy in the class's metadata area (metaspace in modern JVMs) | One copy per object on the heap |
| Created when | The class is loaded by the classloader | The object is constructed with new |
| Garbage collected when | The classloader is garbage collected (rarely, for application classes) | The object is garbage collected |
| Memory cost for N instances | Constant regardless of N | Proportional to N |
Declaring a value private final when it could be private static final stores it inside every object. For an int that is 4 bytes per instance, which is negligible. For a String or a large array, it multiplies quickly in applications that create millions of instances, and it hides the fact that the value is universal.
Choosing between them
private static final for true constants
Values that are the same for every instance and do not depend on constructor arguments belong on the class:
Java constants are conventionally named in UPPER_SNAKE_CASE. The convention applies only to static final fields; instance fields stay camelCase even when they are final.
private final for per-instance immutable state
Values fixed after construction but different between objects belong on the instance:
Each Order has its own orderId and total, and final signals they will not change after construction, which makes the object's state easier to reason about.
A quick test
- Is the value the same for every instance? Use
static final. - Does it depend on constructor arguments or construction-time state? Use
finalwithoutstatic. - Unsure? Ask whether two instances with different values for this field would ever make sense. If not, it is a class-level constant.
final does not make the object immutable
final prevents reassigning the reference, not mutating the object it points to. This holds for both static final and instance final:
For truly immutable contents, use an unmodifiable collection:
List.of() (Java 9+) returns an unmodifiable list, and calling add() on it throws UnsupportedOperationException.
Also note that only static final fields of primitive types or String initialized with a literal are compile-time constants inlined by javac. Fields of other types are initialized at class load time.
Thread safety and leaking this
The JVM guarantees that static final fields initialized at declaration or in a static initializer are safely published to all threads; class loading ensures it.
final instance fields are also safely published, as long as the object reference does not escape during construction (the safe publication guarantee in the Java Memory Model, JSR-133). If you leak this from a constructor, another thread can see an uninitialized final field with its default (zero or null) value:
Do not publish this before the constructor completes.
Related reading
- problem path for truststore inside docker with spring boot and kafka
- Problems using Maven and SSL behind proxy
- Problems with DCT and IDCT algorithm in java
- Process finished with exit code 1 Spring Boot Intellij
- Profile specific custom property files in Spring boot
- Programmatical approach in Java for file comparison
- Programmatically determine which Java thread holds a lock
- Programmatically shut down Spring Boot application
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.