The final keyword is a non-access modifier used to restrict modification. It applies to variables (value cannot change) methods (cannot be overridden) and classes (cannot be extended). It helps create constants, control inheritance and enforce fixed behavior.
The following are different contexts where the final is used:
- Variable
- Method
- Class
final keywordCharacteristics 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.
Final Variable
A variable declared with final becomes constant after one assignment.
Java
public class Geeks {
public static void main(String[] args) {
final double PI = 3.14159;
System.out.println("Value of PI: " + PI);
}
}
OutputValue of PI: 3.14159
Types of Final Variables
1. final Variable
final int THRESHOLD = 5;
2. Blank final Variable
final int THRESHOLD;
3. Static final Variable
static final double PI = 3.141592653589793;
4. Static Blank final Variable
static final double PI;
Initializing Final Variables
A final variable can be initialized in three ways.
- At declaration
- Inside constructors or instance initializer blocks for blank final instance variables
- Inside a static block for blank final static variables
Java
class Geeks {
final int THRESHOLD = 5;
final int CAPACITY;
final int MINIMUM;
static final double PI = 3.141592653589793;
static final double EULER;
{
CAPACITY = 25;
}
static {
EULER = 2.3;
}
Geeks() {
MINIMUM = -1;
}
}
Reference Final Variable
A final reference cannot refer to a new object though the object it points to can change internally.
Java
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);
}
}
OutputGeeks
GeeksForGeeks
This shows non-transitivity. The reference cannot change but the object state can.
Reassigning a Final Variable (Error)
Java
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.
Java
class Geeks {
public static void main(String[] args) {
final int i;
i = 20;
System.out.println(i);
}
}
final in For-Each Loops
Each iteration creates a new variable so using final is valid.
Java
class Geeks {
public static void main(String[] args) {
int arr[] = {1, 2, 3};
for (final int i : arr) {
System.out.print(i + " ");
}
}
}
More Examples of final
Java
public class Geeks {
public static void main(String[] args) {
final int VALUE = 10;
System.out.println("The value is: " + VALUE);
final String MESSAGE = "Hello world";
System.out.println(MESSAGE);
MyClass obj = new MyClass();
obj.printMessage();
obj.printFinalMessage();
}
}
class MyClass {
final String message = "Hello";
void printMessage() {
System.out.println(message);
}
void printFinalMessage() {
final String finalMessage = "Hello final";
System.out.println(finalMessage);
}
}
final class MyOtherClass { }
OutputThe value is: 10
Hello, world!
Hello!
Hello, final!
Final class
A class declared as final cannot be extended.
Java
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.
Final Method
When a method is declared with final keyword, it is called a final method in Java. A final method cannot be overridden.
Java
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
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java