GoBasics

The final Keyword in Java: Lock Variables, Methods & Classes

The final keyword in Java is an important modifier that stops things from being changed. When you use it, the variable, method, or class cannot be changed, overridden, or extended after it is set. This helps make your code more reliable and easier to maintain.

You can use final with:

Let’s walk through each one using beginner-friendly examples. You will also see where most people go wrong.

1. Final Variables – Set Once, Use Forever

A final variable means once you give it a value, you cannot change it later.

final int speed = 60;
System.out.println(speed);

This works perfectly fine. The value is set once and printed.

final int speed = 60;
speed = 80; // Error: cannot assign a value to final variable

That second line will cause a compilation error. You told Java it is final, but then tried to change it. That is not allowed.

Common mistake: People assume they can update a final variable later. You cannot. Set it once, and that is it.

Best practice: When using final variables as constants, write them in ALL_CAPS.

final int MAX_USERS = 100;

2. Final Objects – You Can Change the Inside, But Not the Object Itself

Here is something tricky. If you have a final object, what happens if you try to change it?

final StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
Output: Hello World

That worked even though sb is final. Why? Because you did not change the object itself, only its content.

final StringBuilder sb = new StringBuilder("Hi");
sb = new StringBuilder("Bye"); // Error: cannot reassign a final reference

This causes an error.

Rule: You can change what is inside a final object if it is mutable, but you cannot assign a new object to the variable.

3. Final Methods – You Can Use It, But Cannot Override It

When you make a method final, you are telling others: Do not change this method’s behavior.

class Animal {
  final void sound() {
    System.out.println("Animal sound");
  }
}

class Dog extends Animal {
  void sound() {
    System.out.println("Dog barks"); // Error
  }
}

Java will not allow this.

Common mistake: Trying to override a final method. Java prevents this because final methods cannot be changed.

4. Final Classes – Not Inheritable

Declaring a class as final means no other class can extend it.

final class Calculator {
  int add(int a, int b) {
    return a + b;
  }
}

class AdvancedCalculator extends Calculator { } // Error

Java will give an error.

5. Final Parameters – Locking the Inputs

You can use final with method parameters to prevent changes inside the method.

void greet(final String name) {
  // name = "Alex"; // Error
  System.out.println("Hello " + name);
}

Inside the method, you cannot change the value of name. This protects inputs from accidental changes.

6. Final and Static – Defining Constants

When you combine final with static, the value belongs to the class and never changes.

static final double PI = 3.14159;
System.out.println(PI);
Output: 3.14159

Static means you can use the value without creating an object. Final means the value cannot change.

7. Why Use final?

8. Conclusion

UsageMeaning
final variableCannot assign a new value after initialization
final objectReference cannot change, but contents can if mutable
final methodCannot be overridden in subclasses
final classCannot be extended or inherited
final parameterCannot be reassigned inside the method
static finalConstant shared by all instances, value fixed