Java Final Keyword
The final keyword locks something so nobody can change it. Java lets you lock a variable, a method, or a whole class. Use final when a value or behavior must stay fixed.
An Engraved Stone Example
Writing on paper allows corrections. Carving words into a stone plaque allows none. A final variable works like the plaque. The program sets the value once and the value stays forever.
Final Variables
A final variable accepts one value only. Any later change causes a compile error.
final double PI = 3.14159;
PI = 3.14; // Error: cannot assign a value to final variable
Programmers write final constants in capital letters. Pair final with static to share one constant across the whole program.
public static final int MAX_USERS = 100;
Blank Final Variables
A blank final variable gets its value later, once, inside a constructor.
class Student {
final int rollNo;
Student(int rollNo) {
this.rollNo = rollNo;
}
}
Each student keeps a roll number that never changes.
Final with Objects
A final reference points to one object forever. The object itself can still change inside.
final StringBuilder sb = new StringBuilder("Hi");
sb.append(" there"); // allowed
sb = new StringBuilder(); // Error: cannot point to a new object
Diagram: Locked Pointer
final sb ------locked arrow------> [ "Hi there" ]
^
contents may change,
the arrow may not move
Final Methods
A final method cannot be overridden by a child class. Use it when the parent method contains rules that no child may alter.
class Payment {
final void printReceipt() {
System.out.println("Receipt issued");
}
}
class CardPayment extends Payment {
void printReceipt() { } // Error: cannot override final method
}
Final Classes
A final class cannot be extended. The Java String class is final, so no one can create a child of String and weaken its safety.
final class Config { }
class MyConfig extends Config { } // Error: cannot inherit from final class
Where Final Fits
| Target | Effect |
|---|---|
| Variable | Value stays fixed after the first assignment |
| Reference | Pointer stays fixed, contents may change |
| Method | Child classes cannot override it |
| Class | No child classes allowed |
Final Parameters
A final parameter blocks changes inside the method. Many teams use this habit to prevent accidental edits.
void show(final int count) {
count = 5; // Error
}
Common Mistakes
- Believing final makes an object unchangeable
- Declaring a final variable and never giving it a value
- Marking too many classes final and blocking useful extension
Real Example: Tax Rate
A shopping program applies a fixed tax rate to every bill. Developers mark the rate as a static final constant. A teammate cannot change the rate by accident, and every bill uses the same number.
class Bill {
static final double TAX_RATE = 0.18;
double total(double price) {
return price + price * TAX_RATE;
}
}
Change the rate in one place and the whole program follows. Readers understand the meaning of TAX_RATE faster than a bare number like 0.18 in the middle of a formula.
Quick Summary
- final variable: one assignment only
- final method: no overriding
- final class: no inheritance
- static final creates a program-wide constant
