Stop 04 · Classes & ObjectsTopics · 6/8

final Keyword

Part of the Java Roadmap

In short

final is Java's way of saying "this will not change". It makes code safer, helps the compiler and the reader, and it behaves slightly differently on variables, methods and classes.

final on a variable means it can be assigned only once, on a method means it cannot be overridden, and on a class means it cannot be extended. A final reference can still point to an object whose contents change.

The jaadu presses a heavy rubber stamp onto a document with great effort, an open folder of loose pages lying beside it.

Think of it like this

Writing in permanent ink

Your Aadhaar number is printed once and never reissued. Write it on a form in permanent ink and it cannot be rubbed out. But if the form says "see attached folder", you can still add pages to that folder. A final reference is the ink: it always points at the same folder, while the folder's contents can still change.

Watch it happen

Three meanings of final

final
variable: assign once
method: cannot override
class: cannot extend
final reference: object can still change

1/4On a variable or field: it can be assigned only once.

The big ideas

What you need to know

final variables

Assign once. For fields, assign in the declaration or every constructor.

Reference, not object

final List<String> list can still list.add("x"). It just cannot be pointed to another list.

final methods and classes

Prevent overriding or extending where it would break important rules, like String's immutability.

Prefer final fields

Fields that never change should be final. Immutable objects are simpler and safe to share between threads.

See it in code

Walk through the code

java
1import java.util.ArrayList;2import java.util.List;3 4public final class Citizen {5    private final String aadhaarLast4;6    private final List<String> addresses = new ArrayList<>();7 8    public Citizen(String aadhaarLast4) {9        this.aadhaarLast4 = aadhaarLast4;10    }11 12    public void addAddress(String address) {13        addresses.add(address);          // allowed: changing contents14        // addresses = new ArrayList<>(); // compile error: final reference15    }16 17    public String getAadhaarLast4() { return aadhaarLast4; }18 19    public List<String> getAddresses() { return List.copyOf(addresses); }20 21    public static void main(String[] args) {22        final int maxTries = 3;23        // maxTries = 4;                   // compile error24        Citizen c = new Citizen("4821");25        c.addAddress("Lucknow");26        c.addAddress("Kanpur");27        System.out.println(c.getAddresses() + " tries: " + maxTries);28    }29}

1/5final class: no one can create a subclass of Citizen.

A final class with final fields, a final reference whose list still grows, and a final local variable.

Save yourself the pain

Common mistakes

Oops

Thinking final List<String> makes the list read-only.

Do this instead

Only the variable is fixed. Use List.of or List.copyOf for a list that cannot be changed.

Oops

Confusing final with finally and finalize.

Do this instead

final is a modifier. finally is a block after try that always runs. finalize is a deprecated cleanup method; do not use it.

Your mission

Make it immutable

~20 min

Quick check

Given final int[] arr = {1, 2, 3};, which line compiles?

Go deeper

  1. 1Declare a constant with static final and use it in calculations.
  2. 2Make a final List, add items to it and explain why that still works.
  3. 3Mark a class final and try to extend it to see the error.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge