Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Java final Keyword

The final keyword in Java means "this cannot be changed further," but what exactly gets locked down depends on where it is applied: a variable, a method, or a class. All three uses share the same underlying idea of preventing modification after the fact, applied at three different levels.

Why it matters

final states that something will not change: a variable is assigned once, a method is not overridden, a class is not extended. It makes intent clear to readers and lets the compiler enforce it. The detail that surprises people is that a final variable holding an object does not make that object immutable.

Watch: Java final Keyword Explained 🔒 final Variable, Method & Class with Examples | Java Core

A detailed walkthrough of Java’s final keyword on variables, methods, and classes, with examples of how it restricts reassignment and inheritance.

Watch on YouTube

final Variable — a Constant

A final variable can be assigned a value exactly once. For a primitive, that means its value can never change afterward; for a reference type, it means the variable can never be reassigned to point at a different object, although the object it points to can still be mutated internally if that object itself is mutable. By convention, final constants are named in UPPER_SNAKE_CASE.

final Method — Cannot Be Overridden

A final method can be inherited and called by subclasses, but it cannot be overridden. This is useful when a superclass needs to guarantee that a piece of logic behaves identically in every subclass, such as a security check or a core algorithm step that must not be altered.

final Class — Cannot Be Extended

A final class cannot be subclassed at all; attempting extends on it is a compile-time error. The standard library uses this for classes whose behavior must never be altered by inheritance — String, Integer, and the other wrapper classes are all declared final.

Example

final applied to a variable, a method, and a class in one hierarchy

Java
class Payment {
    static final double TAX_RATE = 0.08; // final variable, a constant

    final double totalWithTax(double amount) { // final method, cannot be overridden
        return amount + (amount * TAX_RATE);
    }
}

final class CreditCardPayment extends Payment {
    // final class — CreditCardPayment itself cannot be extended further
}

public class FinalDemo {
    public static void main(String[] args) {
        CreditCardPayment payment = new CreditCardPayment();
        System.out.println(payment.totalWithTax(100.0));
    }
}
Output
108.0

Common Mistakes

  • Trying to reassign a final variable after its first assignment, which is always a compile error.
  • Assuming a final reference variable makes the object it points to immutable — final only locks the reference, not the object's internal state.
  • Attempting to override a final method in a subclass and being surprised by a compile-time error rather than a runtime one.
  • Trying to extend a final class such as String, which the compiler rejects outright.

Exercise

This program does not compile, because it reassigns a final variable. Remove the illegal line, keep the legal one, and notice that changing an element of a final array is allowed.

Expected output: [10, 2, 3]

Java · starter code
import java.util.Arrays;

public class FinalDemo {
    public static void main(String[] args) {
        final int[] scores = {1, 2, 3};

        scores[0] = 10;
        scores = new int[] {7, 8, 9};

        System.out.println(Arrays.toString(scores));
    }
}
Show hints
  • final stops the variable from being pointed at a different array.
  • It does not stop the contents of the array from being changed.
Show solution
Java · solution
import java.util.Arrays;

public class FinalDemo {
    public static void main(String[] args) {
        final int[] scores = {1, 2, 3};

        scores[0] = 10;

        System.out.println(Arrays.toString(scores)); // [10, 2, 3]
    }
}

Quick quiz

1. What does final mean on a method?

  • A. It cannot be called
  • B. It cannot be overloaded
  • C. It cannot be overridden by a subclass
  • D. It must return a constant
Show answer

C. A final method keeps its implementation in every subclass. It can still be called and overloaded.

2. What does final mean on a class?

  • A. It cannot be instantiated
  • B. It cannot be extended
  • C. All its fields are constants
  • D. It has no methods
Show answer

B. A final class cannot have subclasses. String is the best-known example.

3. Given final List<String> names = new ArrayList<>();, what happens with names.add("Asha");?

  • A. It works; final only prevents reassigning the variable
  • B. It does not compile
  • C. It throws UnsupportedOperationException
  • D. It is ignored
Show answer

A. The reference cannot be changed, but the list it refers to is an ordinary mutable ArrayList.

Interview questions

What is a blank final variable?

Show answer

A final field declared without an initial value. It must be assigned exactly once before the constructor finishes, usually inside the constructor, and can never be changed afterwards. This lets each object have its own constant value, such as an id passed in at creation.

What does "effectively final" mean?

Show answer

A local variable that is never reassigned after its first assignment is effectively final, even without the keyword. Lambdas and anonymous classes may only use local variables that are final or effectively final, which is why changing a captured variable inside or after a lambda causes a compile error.

Key Points to Remember

  • final on a variable means single assignment, effectively a constant.
  • final on a method prevents subclasses from overriding it.
  • final on a class prevents any subclassing at all.
  • A final reference still allows the referenced object's own fields to change.

Go deeper

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering