In Java, the final keyword is used to restrict changes and make code more secure and predictable. It can be applied to variables, methods, and classes to prevent modification, overriding, or inheritance. This helps in creating constant values, stable methods, and immutable classes.
- Final variable cannot be changed once assigned
- Final method cannot be overridden
- Final class cannot be inherited
The following are different contexts where the final is used:

Characteristics of final keyword in Java
- Final variables hold a value that cannot be reassigned after initialization.
- Final methods cannot be overridden by subclasses.
- Final classes cannot be extended.
- Initialization rules require that a final variable must be assigned exactly once either at declaration or inside constructors or initializer blocks.
- Reference final variables cannot change which object they point to though the internal state of the object can change.
- Static final variables represent constants shared across all objects.
- Blank final variables are declared without initialization and must be assigned once before use.
- Local final variables inside methods must be initialized within their block.
Ways we use the final keyword in Java
The final keyword is used in exactly 3 main contexts:
1. Final Variable
A variable declared with final becomes constant after one assignment.
public class Geeks{
public static void main(String[] args) {
final double PI = 3.14159;
System.out.println("Value of PI: " + PI);
}
}
Output
Value of PI: 3.14159
Types of Final Variables
A variable declared with final becomes constant after one assignment.
1. final Variable
final int THRESHOLD = 5;
2. Blank final Variable
final int THRESHOLD;
Note: A static final variable must be initialized either at the point of declaration or in a static initialization block.
3. Static final Variable
static final double PI = 3.141592653589793;
4. Static Blank final Variable
static final double PI;
static {
PI = 3.141592653589793;
}
Reference Final Variable
A final reference cannot refer to a new object though the object it points to can change internally.
class Geeks {
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder("Geeks");
System.out.println(sb);
sb.append("ForGeeks");
System.out.println(sb);
}
}
Output
Geeks GeeksForGeeks
This shows that a final reference cannot point to a different object, but the internal state of the object it points to can still be modified.
Reassigning a Final Variable (Error)
class Geeks {
static final int CAPACITY = 4;
public static void main(String[] args) {
CAPACITY = 5; // compile-time error
}
}
Output:

Local Final Variable
A local final variable must be assigned once.
class Geeks {
public static void main(String[] args) {
final int i;
i = 20;
System.out.println(i);
}
}
Output
20
2. Final class
A class declared as final cannot be extended. A final class cannot be inherited, meaning no other class can extend it. This is commonly used for security or when the implementation should remain unchanged, such as in core classes like String.
- Prevents inheritance, ensuring the class behavior cannot be modified
- Commonly used for immutable and secure classes
final class A {
// fields and methods
}
// Illegal
class B extends A { }
Final classes are useful when creating immutable classes such as String or wrapper classes.
3. Final Method
When a method is declared with final keyword, it is called a final method in Java.A final method, on the other hand, can be inherited but cannot be overridden by subclasses, ensuring that the original implementation remains intact.
- Cannot be overridden in subclasses
- Helps maintain consistent behavior across all derived classes
class A {
final void m1() {
System.out.println("Final method");
}
}
class B extends A {
void m1() { } // compile-time error
}
Advantages of final Keyword
- Supports immutability by preventing reassignment
- Helps compiler and JVM optimize code in some scenarios
- Makes behavior predictable since values or methods stay unchanged
- Prevents accidental or unauthorized modification of critical logic
- Preserves API contracts by avoiding unwanted overriding