The final keyword in Java is used to restrict modification.
In simple words:
final keyword is used to make variables constant, prevent method overriding, and stop inheritance.
Why final Keyword is Important?
The final keyword helps:
- Improve security
- Prevent unwanted modifications
- Create immutable values
- Protect business logic
- Improve application stability
Main Uses of final Keyword
- Final Variable
- Final Method
- Final Class
final Keyword Architecture Diagram
final Keyword
|
+-------> final Variable
|
+-------> final Method
|
+-------> final Class
1. final Variable in Java
A final variable cannot be modified once initialized.
Example
class Test {
final int x = 100;
}
Invalid Modification
x = 200;
Compiler Error Reason
Because final variables become constants after initialization.
final Variable Flow
Variable Created
|
v
Value Assigned Once
|
v
Modification Not Allowed
Blank final Variable
A blank final variable is declared first and initialized later.
Example
class Employee {
final int id;
Employee(int id) {
this.id = id;
}
}
Why Useful?
Useful when value depends on constructor input.
final Static Variable
final static variables are constants shared across all objects.
Example
class MathUtil {
static final double PI = 3.14159;
}
Why static final is Common?
- Single memory copy
- Immutable value
- Fast access
- Ideal for constants
2. final Method in Java
A final method cannot be overridden by child classes.
Example
class Parent {
final void display() {
System.out.println("Final Method");
}
}
class Child extends Parent {
}
Invalid Overriding
class Child extends Parent {
void display() {
}
}
Compiler Error Reason
Final methods prevent overriding.
final Method Flow
Parent Method
|
v
Marked final
|
v
Child Cannot Override
Why final Methods are Used?
- Protect core business logic
- Improve security
- Maintain fixed behavior
- Prevent accidental overriding
Realtime Example
Banking systems may use final methods for:
- Transaction validation
- Security checks
- Audit verification
Example
final void validateTransaction() {
}
3. final Class in Java
A final class cannot be inherited.
Example
final class SecurityManager {
}
Invalid Inheritance
class CustomSecurity
extends SecurityManager {
}
Compiler Error Reason
Final classes cannot be extended.
final Class Flow
Class Declared final
|
v
Inheritance Blocked
|
v
Class Behavior Protected
Why final Classes are Used?
- Security
- Immutable design
- Prevent modification
- Stable implementations
Real-Time Example
Java String class is final:
public final class String
Why String is final?
- Security
- Immutability
- Thread safety
- Performance optimization
final Keyword Memory Flow
Class Loaded
|
v
final Members Initialized
|
v
Modification Restricted
Difference Between final, finally, and finalize
| Feature | final | finally | finalize() |
|---|---|---|---|
| Type | Keyword | Block | Method |
| Purpose | Restriction | Exception Handling | Garbage Collection |
| Used With | Variable/Class/Method | try-catch | Object Cleanup |
final vs Immutable
final reference variable cannot change reference, but object data may still change.
Example
final Listlist = new ArrayList<>(); list.add("Java");
Why Allowed?
Reference remains same, but object contents change.
Immutable Object Example
final String name = "Java";
String object itself is immutable.
final Keyword in Banking Systems
Banking applications use final keyword for:
- Security rules
- Transaction validation
- Immutable account configurations
- Audit logging frameworks
Example
final double MIN_BALANCE = 1000;
final Keyword in E-Commerce Systems
E-commerce platforms use final keyword for:
- Tax constants
- Discount rules
- Immutable order IDs
- Secure payment validation
final Keyword in Spring Boot
Spring Boot applications heavily use final keyword for:
- Dependency injection
- Immutable services
- Configuration constants
- Thread safety
Spring Boot Constructor Injection Example
@Service
class UserService {
private final UserRepository repo;
UserService(UserRepository repo) {
this.repo = repo;
}
}
Why final Used Here?
Dependency reference should never change after initialization.
final Keyword in Microservices
Microservices architectures use final keyword for:
- Immutable configuration
- Security policies
- Shared constants
- Stable APIs
Advantages of final Keyword
- Improves security
- Supports immutability
- Prevents accidental modification
- Improves code reliability
- Supports thread safety
Disadvantages of final Keyword
- Reduces flexibility
- Prevents extensibility
- May complicate testing in some scenarios
Common Interview Mistake
Many developers think final objects become immutable automatically.
Actually:
- final reference cannot change.
- Object data may still change unless object itself is immutable.
Another Common Mistake
Many developers think final methods improve performance significantly.
Actually:
- Main purpose is restriction and security, not performance.
Best Practices
- Use final for constants
- Use final dependencies in Spring Boot
- Use final methods for sensitive logic
- Prefer immutable design when possible
Realtime Enterprise Example
Payment Configuration
static final String API_KEY =
"secure-key";
API key should never change during runtime.
Related Learning Topics
- Static Variables and Methods in Java
- What is this Keyword in Java
- What is super Keyword in Java
- What is Method Overriding
- What is Constructor Chaining
- OOPS in Java
- Memory Management in Java
- How JVM Works Internally
- What is Spring Boot
Professional Interview Answer
The final keyword in Java is used to restrict modification. A final variable becomes constant once initialized, a final method cannot be overridden, and a final class cannot be inherited. The final keyword is widely used to improve security, maintain immutability, protect business logic, and create stable APIs. Enterprise applications, Spring Boot frameworks, banking systems, and microservices architectures heavily use final variables for constants and dependency injection, final methods for secure logic, and final classes for immutable implementations.
Frequently Asked Questions
What is final keyword in Java?
final is a keyword used to restrict modification of variables, methods, and classes.
Can final variables be modified?
No, final variables cannot be modified after initialization.
Can final methods be overridden?
No, final methods cannot be overridden.
Can final classes be inherited?
No, final classes cannot be extended.
Why is String class final?
To maintain immutability, security, and thread safety.