final Keyword
The final keyword in Java is used to restrict modification. It is applied to variables, methods, and classes to enforce immutability, prevent overriding, and stop inheritance.
This is a high-frequency interview topic and very important for writing safe, predictable Java code.
What Does final Mean?
- final means cannot be changed
- Enforces restrictions at compile time
- Improves security, performance, and design clarity
Uses of final Keyword in Java
The final keyword can be used with:
- Variables
- Methods
- Classes
1. final Variable
What Is a Final Variable?
- Value cannot be changed once assigned
- Acts like a constant
final int MAX_LIMIT = 100;
Rules for Final Variables
- Must be initialized only once
- Can be initialized:
- At declaration
- In constructor (for instance final variables)
Example: Final Variable
final int x = 10;
x = 20; // ❌ compilation error
Final Instance Variable
class Test {
final int value;
Test(int value) {
this.value = value; // allowed
}
}
Final Static Variable (Constant)
class Config {
static final String APP_NAME = "SoftwareTips4U";
}
Best Practice: Use UPPER_CASE naming for constants.
2. final Method
What Is a Final Method?
- A method declared as final cannot be overridden
- Inheritance is allowed, overriding is not
class Parent {
final void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
void display() { // ❌ compilation error
}
}
Why Use Final Methods?
- Prevent modification of critical logic
- Improve security
- Used in framework/base classes
Example: Security-related validation methods.
3. final Class
What Is a Final Class?
- A final class cannot be inherited
- No subclassing allowed
final class Utility {
void help() {}
}
class MyUtil extends Utility { // ❌ compilation error
}
Why Use Final Classes?
- Prevent unwanted inheritance
- Improve immutability and security
Real-Time Example: String class is final.
Important Clarification (Very Common Interview Trap)
final vs Immutability
final StringBuilder sb = new StringBuilder("Java");
sb.append(" Rocks"); // ✅ allowed
- final → reference cannot change
- Object can change (if mutable)
final with Parameters
void calculate(final int x) {
x = 20; // ❌ not allowed
}
Why it matters: Prevents accidental modification inside methods.
final vs finally vs finalize
| Keyword | Purpose |
|---|---|
| final | Restricts modification |
| finally | Executes cleanup code |
| finalize() | Called by GC (deprecated) |
Common Beginner Mistakes
- Assuming final makes objects immutable
- Forgetting to initialize final variables
- Trying to override final methods
- Confusing final with finally
- Overusing final unnecessarily
Interview-Ready Answers
Short Answer
The final keyword is used to restrict modification of variables, methods, and classes in Java.
Detailed Answer
In Java, final variables cannot be reassigned, final methods cannot be overridden, and final classes cannot be inherited. It is used to enforce immutability, protect logic, and improve code safety.
Key Takeaway
The final keyword is a design and safety tool. It helps write secure, predictable, and maintainable Java code when used appropriately.