Java Reference Library →

final Keyword Usage and Examples

final Keyword Usage and Examples

Learn how the final keyword works in Java for variables, methods, and classes. Understand immutability, inheritance restrictions, and real-world examples with code and output.

This reference explains final Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

final Keyword in Java

In Java, the final keyword is a powerful modifier that brings immutability and restriction to your code. It's used to signal that something should not be changed — whether it’s a variable, a method, or even an entire class. Think of it as Java’s way of saying: "Hands off!"

1. Final Variables – Making Data Immutable

When you declare a variable as final, you are telling Java: “This variable’s value should be assigned only once.” Once a final variable is assigned, it cannot be modified again.

public class FinalVariableExample {
  public static void main(String[] args) {
    final int speedLimit = 60;
    System.out.println("Speed Limit: " + speedLimit);

    // speedLimit = 80; // Uncommenting this will cause a compile-time error
  }
}
Speed Limit: 60

Explanation: The variable speedLimit is marked as final. Attempting to change its value results in a compilation error. This is especially useful for constants or values you want to protect from accidental changes.

2. Final with Reference Types

With objects, final prevents reassignment — but does not make the object immutable.

class Car {
  String model = "Honda";
}

public class FinalReferenceExample {
  public static void main(String[] args) {
    final Car myCar = new Car();
    myCar.model = "Toyota"; // Allowed
    // myCar = new Car(); // Not allowed
    System.out.println("Car model: " + myCar.model);
  }
}
Car model: Toyota

Explanation: The reference myCar is final, so it cannot point to another Car object. However, the contents of the object (like its model) can still be changed.

3. Final Methods – Locking Down Behavior

When you mark a method as final, it cannot be overridden by subclasses. This ensures that the core logic of the method remains untouched — ideal for methods that form the backbone of a class’s behavior.

class Parent {
  final void show() {
    System.out.println("Final method in Parent");
  }
}

class Child extends Parent {
  // void show() { System.out.println("Trying to override"); } // Error!
}
Final method in Parent

Explanation: The method show() is declared final in the parent class, so any attempt to override it in a child class will lead to a compile-time error.

4. Final Classes – Preventing Inheritance

A final class is the ultimate boundary — it cannot be extended or subclassed. This is commonly seen in utility or security-related classes.

final class Vehicle {
  void drive() {
    System.out.println("Driving safely...");
  }
}

// class Bike extends Vehicle {} // Compile-time error
Driving safely...

Explanation: Once a class is marked final, it’s sealed. It can still be instantiated, but no subclassing is allowed — this ensures the class's design is preserved as-is.

5. Final Parameters – Read-Only Inputs

You can also declare method parameters as final to ensure that their values are not reassigned within the method.

public class FinalParameterExample {
  void display(final int x) {
    System.out.println("Value: " + x);
    // x = 50; // Not allowed
  }
}
Value: [whatever value is passed]

Explanation: This is useful for making sure that parameters passed to methods remain consistent, especially in recursive logic or callback scenarios.

When to Use final

  • To define constants (e.g., final double PI = 3.14;)
  • To avoid accidental modification of variables
  • To ensure method behavior is locked for subclasses
  • To improve code clarity and intent
  • To make classes immutable and non-inheritable

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →