The final keyword is a modifier used to restrict certain changes to variables, methods, and classes. It helps enforce design constraints and prevent unintended modifications.
- A final variable can be assigned only once.
- A final method cannot be overridden by a subclass.
- A final class cannot be extended.
Types of Final Keyword
The final keyword can be used with three main components in Java.
1. Final Variable in Java
A variable declared with the final keyword cannot be reassigned after it has been initialized.
Syntax:
final dataType variableName = value;
class Geeks{
public static void main(String[] args){
final double PI = 3.14159;
System.out.println("Value of PI: " + PI);
// PI = 3.14; // Compile-time error
}
}
Output
Value of PI: 3.14159
Explanation: The variable PI is initialized with a value that cannot be reassigned. Attempting to assign another value causes a compile-time error.
Types of Final Variables
Final variables can be declared and initialized in different ways.
Type | Description |
|---|---|
Initialized final variable | Assigned during declaration. |
Blank final variable | Declared without an initial value and assigned later. |
Static final variable | Belongs to the class and can be assigned only once. |
Static blank final variable | Initialized during class initialization, typically in a static block. |
Final Reference Variables
When a reference variable is declared final, it cannot refer to another object after assignment. However, the referenced object's internal state may still change.
class Geeks{
public static void main(String[] args){
final StringBuilder sb = new StringBuilder("Geeks");
// Modify the existing object
sb.append("ForGeeks");
System.out.println(sb);
// sb = new StringBuilder("Java"); // Error
}
}
Output
GeeksForGeeks
Explanation: The append() method modifies the existing StringBuilder object. The final keyword prevents reassignment of sb, not modification of the object.
2. Final Method
A final method can be inherited and called by a subclass, but the subclass cannot override it.
class Parent{
// Final method
final void display(){
System.out.println("Final method");
}
}
class Child extends Parent{
// display() cannot be overridden here
}
public class Geeks{
public static void main(String[] args){
Child obj = new Child();
obj.display();
}
}
Output
Final method
Explanation: The Child class inherits the final display() method from Parent and can invoke it without overriding its implementation.
3. Final Class
A final class cannot be extended by another class. It is useful when inheritance should not be allowed.
final class Student{
void display(){
System.out.println("Student class");
}
}
public class Geeks{
public static void main(String[] args){
Student obj = new Student();
obj.display();
}
}
Output
Student class
Explanation: The Student class is declared final, so another class cannot extend it. However, objects of Student can still be created normally.
Note: Declaring a class final does not automatically make its objects immutable or secure. It only prevents inheritance.
Difference Between final Variable, Method, and Class
Usage | Restriction |
|---|---|
Final Variable | Prevents reassignment after initialization. |
Final Method | Prevents overriding in subclasses. |
Final Class | Prevents inheritance. |