Java Programming
Static Keyword
Final Keyword
Programming Concepts
Object-Oriented Programming

Difference between Static and final?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, static and final are both modifiers, but they answer completely different questions. static is about class-level ownership and lifecycle. final is about things that should not be reassigned, overridden, or extended. Confusing them leads to APIs that are harder to reason about and harder to test.

static Means the Member Belongs to the Class

A static field or method is attached to the class itself rather than to any particular instance. Every object shares the same static field, and static methods can be called without creating an instance.

java
1class Counter {
2    static int total = 0;
3
4    Counter() {
5        total++;
6    }
7}
8
9public class Demo {
10    public static void main(String[] args) {
11        new Counter();
12        new Counter();
13        System.out.println(Counter.total);
14    }
15}

Here total is shared state. That is the core meaning of static: one class-level value, not one value per object.

final Means the Contract Cannot Change in That Way

final depends on where you use it:

  • A final variable cannot be reassigned.
  • A final method cannot be overridden.
  • A final class cannot be subclassed.
java
1final class Config {
2    final int timeoutMs;
3
4    Config(int timeoutMs) {
5        this.timeoutMs = timeoutMs;
6    }
7}

This code says two things: Config is not meant to be extended, and timeoutMs cannot point to a different integer value after construction.

static final Is Common for Constants

Because the two modifiers solve different problems, they are often combined. A constant that is shared by the entire class is naturally static final.

java
1class Limits {
2    public static final int MAX_RETRIES = 3;
3    public static final String SERVICE_NAME = "Billing";
4}

This is a class-scoped constant. It does not vary by instance, and it should not be reassigned.

final Does Not Automatically Mean Deep Immutability

One of the most common misconceptions is that final makes an object fully immutable. It does not. A final reference cannot be changed to point somewhere else, but the object it points to may still be mutable.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class NamesDemo {
5    public static void main(String[] args) {
6        final List<String> names = new ArrayList<>();
7        names.add("Ava");
8        names.add("Noah");
9        System.out.println(names);
10    }
11}

The names variable is final, but the list contents still change. If deep immutability matters, you need immutable objects or defensive copying, not just the final keyword.

static Does Not Mean Thread-Safe

Another common mistake is assuming class-level state is somehow safer or more controlled. Shared mutable static fields can be some of the hardest state to manage because every instance and every thread can reach them.

java
1import java.util.concurrent.atomic.AtomicInteger;
2
3class SharedCounter {
4    static final AtomicInteger TOTAL = new AtomicInteger(0);
5}

If shared state must exist, it still needs the right concurrency strategy. static changes ownership, not synchronization.

Use the Modifier That Matches the Design Goal

Use static when the value or behavior is genuinely class-wide, such as utility functions, constants, or carefully managed shared infrastructure. Use final when you need a stronger contract around reassignment or inheritance. If you are reaching for static just to avoid dependency injection, that is often a design smell. If you are reaching for final without being clear about whether you want shallow or deep immutability, the code may still be misleading.

In code review, the best question is not "can this compile with static or final." It is "what guarantee are we trying to express."

Common Pitfalls

The usual mistakes are treating static state as if it were automatically safe, treating final references as if they implied deep immutability, and adding final or static by habit instead of design intent. Teams also create hard-to-test global state with mutable static fields when instance-based dependencies would have been clearer.

Summary

  • 'static means class-level ownership and shared lifecycle.'
  • 'final means no reassignment, no override, or no subclassing, depending on location.'
  • 'static final is the standard combination for shared constants.'
  • 'final does not guarantee deep immutability.'
  • 'static does not provide thread safety by itself.'

Course illustration
Course illustration

All Rights Reserved.