Java
Coding Standards
Object-Oriented Programming
Variable Declaration
Programming Concepts

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

ModifierEffect
privateRestricts access to the declaring class only
finalPrevents reassignment after initialization
staticBinds 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

java
1public class Connection {
2    // Class-level constant: one copy, shared by all instances
3    private static final int MAX_RETRIES = 3;
4
5    // Instance-level constant: unique per object, set at construction
6    private final String host;
7
8    public Connection(String host) {
9        this.host = host;
10    }
11}

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

java
1public class Example {
2    // static final: must be assigned in declaration or static initializer
3    private static final String CONFIG_PATH;
4    static {
5        CONFIG_PATH = System.getenv("APP_CONFIG");
6    }
7
8    // final: must be assigned in declaration, instance initializer, or constructor
9    private final long createdAt;
10
11    public Example() {
12        this.createdAt = System.currentTimeMillis();
13    }
14}

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

Aspectprivate static finalprivate final
StorageOne copy in the class's metadata area (metaspace in modern JVMs)One copy per object on the heap
Created whenThe class is loaded by the classloaderThe object is constructed with new
Garbage collected whenThe classloader is garbage collected (rarely, for application classes)The object is garbage collected
Memory cost for N instancesConstant regardless of NProportional 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.

java
1// Wasteful: every instance carries its own identical copy
2public class BadExample {
3    private final String DEFAULT_REGION = "us-east-1";
4}
5
6// Correct: one shared constant
7public class GoodExample {
8    private static final String DEFAULT_REGION = "us-east-1";
9}

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
1public class MathUtils {
2    private static final double TAX_RATE = 0.075;
3    private static final int BUFFER_SIZE = 8192;
4    private static final String API_VERSION = "v2";
5}

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:

java
1public class Order {
2    private final String orderId;
3    private final LocalDateTime placedAt;
4    private final BigDecimal total;
5
6    public Order(String orderId, BigDecimal total) {
7        this.orderId = orderId;
8        this.placedAt = LocalDateTime.now();
9        this.total = total;
10    }
11}

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

  1. Is the value the same for every instance? Use static final.
  2. Does it depend on constructor arguments or construction-time state? Use final without static.
  3. 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:

java
1public class Registry {
2    // The list reference cannot be reassigned, but the list contents can change
3    private static final List<String> ALLOWED_HOSTS = new ArrayList<>();
4
5    static {
6        ALLOWED_HOSTS.add("example.com");
7        ALLOWED_HOSTS.add("api.example.com");
8    }
9
10    public void addHost(String host) {
11        // This compiles and runs without error
12        ALLOWED_HOSTS.add(host);
13    }
14}

For truly immutable contents, use an unmodifiable collection:

java
private static final List<String> ALLOWED_HOSTS =
    List.of("example.com", "api.example.com");

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:

java
1public class Unsafe {
2    private final int value;
3
4    public Unsafe(Registry registry) {
5        registry.register(this); // leaks 'this' before construction finishes
6        this.value = 42;         // another thread reading via registry might see 0
7    }
8}

Do not publish this before the constructor completes.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms