The final keyword is used to declare constants or prevent modification.
It’s meaning depends on where its used.

Final Usage Summary

Use WithEffect
VariableValue cant be changed (constant)
MethodMethod cant be overridden
ClassClass can’t be extended/inherited

Example

Final Variable
final int x = 20;
x = 30; // Compilation error
Final Method
class Animal {
    final void sleep() {
        System.out.println("Sleeping...");
    }
}
 
class Dog extends Animal {
    // Cannot override sleep()
    // void sleep() { ... } β†’ Error
}
Final Class
final class Utility {
    // no one can extend this class
}
 
class MyUtil extends Utility {} // Error
Final Object Reference
final List<String> list = new ArrayList<>();
list.add("Hello");        // Allowed
list = new ArrayList<>(); // Error

Final object reference = can’t reassign.
But the object itself can still be modified (unless immutable).

Tip

Be ready to explain:

  • difference between final on primitive vs object reference,
  • when to use final for immutability, security or clarity,
  • why String is final in Java (thread-safety, caching, security).

Parent: _Core