The `final` Keyword in Java
The final keyword in Java is used to restrict certain modifications in your code. It can be applied to variables, methods, and classes, with different effects in each case. 1. Final Variables When applied to a variable, final means the variable cannot be reassigned after initialization. Types of final variables: Final local variables: Cannot be modified after initialization Final instance variables: Must be initialized either at declaration or in constructor Final static variables (constants): Typically declared with static final and named in ALL_CAPS final int MAX_VALUE = 100; // Final local variable // MAX_VALUE = 200; // Compile error - cannot reassign class Example { final int instanceVar; // Final instance variable Example() { instanceVar = 42; // Must be initialized in constructor } static final double PI = 3.14159; // Constant } 2. Final Methods When applied to a method, final prevents the method from being overridden in subclasses. class Parent { final void display() { System.out.println("This cannot be overridden"); } } class Child extends Parent { // @Override // void display() { } // Compile error - cannot override final method } 3. Final Classes When applied to a class, final prevents the class from being extended (no subclasses allowed). final class ImmutableClass { // Class implementation } // class ExtendedClass extends ImmutableClass { } // Compile error Important Notes about final: Final reference variables: The reference cannot change, but the object's state can still be modified final List names = new ArrayList(); names.add("Alice"); // Allowed - modifying object state // names = new ArrayList(); // Not allowed - changing reference Final parameters: Method parameters can be declared final to prevent reassignment void process(final int input) { // input = 5; // Compile error } Performance benefits: The JVM can optimize final variables better Thread safety: Final variables are thread-safe and don't require synchronization Blank final variables: Must be initialized exactly once before use class Example { final int blankFinal; Example(int value) { blankFinal = value; } } The final keyword is particularly important for: Creating immutable objects Defining constants Preventing unintended modifications Designing secure and thread-safe classes Improving code clarity by showing intent

The final
keyword in Java is used to restrict certain modifications in your code. It can be applied to variables, methods, and classes, with different effects in each case.
1. Final Variables
When applied to a variable, final
means the variable cannot be reassigned after initialization.
Types of final variables:
- Final local variables: Cannot be modified after initialization
- Final instance variables: Must be initialized either at declaration or in constructor
-
Final static variables (constants): Typically declared with
static final
and named in ALL_CAPS
final int MAX_VALUE = 100; // Final local variable
// MAX_VALUE = 200; // Compile error - cannot reassign
class Example {
final int instanceVar; // Final instance variable
Example() {
instanceVar = 42; // Must be initialized in constructor
}
static final double PI = 3.14159; // Constant
}
2. Final Methods
When applied to a method, final
prevents the method from being overridden in subclasses.
class Parent {
final void display() {
System.out.println("This cannot be overridden");
}
}
class Child extends Parent {
// @Override
// void display() { } // Compile error - cannot override final method
}
3. Final Classes
When applied to a class, final
prevents the class from being extended (no subclasses allowed).
final class ImmutableClass {
// Class implementation
}
// class ExtendedClass extends ImmutableClass { } // Compile error
Important Notes about final
:
- Final reference variables: The reference cannot change, but the object's state can still be modified
final List<String> names = new ArrayList<>();
names.add("Alice"); // Allowed - modifying object state
// names = new ArrayList<>(); // Not allowed - changing reference
- Final parameters: Method parameters can be declared final to prevent reassignment
void process(final int input) {
// input = 5; // Compile error
}
Performance benefits: The JVM can optimize final variables better
Thread safety: Final variables are thread-safe and don't require synchronization
Blank final variables: Must be initialized exactly once before use
class Example {
final int blankFinal;
Example(int value) {
blankFinal = value;
}
}
The final
keyword is particularly important for:
- Creating immutable objects
- Defining constants
- Preventing unintended modifications
- Designing secure and thread-safe classes
- Improving code clarity by showing intent