Understanding Java’s final Keyword: Variables, Methods, and Classes
Java final Keyword
Explore how Java’s final keyword safeguards constants, prevents method overriding, and blocks class inheritance, with clear examples.
In Java, the final keyword signals that an entity—be it a variable, method, or class—cannot be altered after its initial definition. This immutable contract is enforced at compile time, providing developers with guarantees about program behavior.
- Final variables cannot be reassigned.
- Final methods cannot be overridden in subclasses.
- Final classes cannot be extended.
1. Final Variables
When you declare a variable as final, its value is locked after the first assignment. This is the standard way to define constants in Java.
class Main {
public static void main(String[] args) {
// Declare a final variable
final int AGE = 32;
// The following line would cause a compilation error
// AGE = 45;
System.out.println("Age: " + AGE);
}
}
The compiler will refuse to compile code that attempts to reassign AGE, issuing an error similar to:
cannot assign a value to final variable AGE
AGE = 45;
^
Tip: By convention, constant names are written in all uppercase letters with underscores (e.g., MAX_SIZE) to signal their immutability.
2. Final Methods
Final methods are part of Java’s inheritance safety net. When a method is marked final, no subclass can override it, preserving the original implementation.
class FinalDemo {
// Final method
public final void display() {
System.out.println("This is a final method.");
}
}
class Main extends FinalDemo {
// Attempt to override – results in a compilation error
public final void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}
Trying to override the display() method will produce an error such as:
display() in Main cannot override display() in FinalDemo
public final void display() {
^
overridden method is final
3. Final Classes
A final class cannot be subclassed. This is often used for security-critical or immutable types, ensuring that the class’s behavior remains unchanged.
// Final class definition
final class FinalClass {
public void display() {
System.out.println("This is a final class.");
}
}
// Attempting to extend the final class
class Main extends FinalClass {
public void display() {
System.out.println("The final class is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}
The compiler will reject this inheritance with:
cannot inherit from final FinalClass
class Main extends FinalClass {
^
Java
- C# Static Keyword: Mastering Static Variables, Methods, and Classes
- Java instanceof Operator: A Comprehensive Guide
- Mastering Java Inheritance: Concepts, Types, and Practical Examples
- Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
- Mastering Java’s ObjectInputStream: A Comprehensive Guide
- Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
- Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
- Mastering Java Reader Class: Subclasses, Methods, and Practical Example
- Mastering Java Generics – Building Reusable, Type‑Safe Code
- Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion