Final Variable vs Final Method vs Final Class in Java: Key Differences
final variable is a constant whose value cannot be changed once assigned, a final method cannot be overridden by subclasses, and a final class cannot be extended by any other class. Each serves to restrict modification at different levels: variables, methods, and classes respectively.Quick Comparison
This table summarizes the key differences between final variable, final method, and final class in Java.
| Aspect | Final Variable | Final Method | Final Class |
|---|---|---|---|
| Purpose | Make variable value constant | Prevent method overriding | Prevent class inheritance |
| Modification | Value cannot be changed after initialization | Method implementation cannot be changed in subclass | Class cannot have subclasses |
| Usage Level | Variable | Method | Class |
| Inheritance Impact | No effect on inheritance | Restricts method override in subclasses | Blocks subclass creation |
| Example Use Case | Constants like PI | Security-sensitive methods | Immutable utility classes |
Key Differences
A final variable in Java is used to create constants. Once you assign a value to a final variable, you cannot change it. This is useful when you want to protect important values from accidental modification, like mathematical constants or configuration values.
A final method is a method that cannot be overridden by any subclass. This means the behavior defined in the method is fixed and subclasses must use it as is. This is helpful when you want to ensure certain logic remains consistent and secure across all subclasses.
A final class cannot be extended by any other class. This means no subclass can inherit from it. This is often used to create immutable classes or utility classes where inheritance could cause problems or security risks.
Code Comparison: Final Variable
This example shows how a final variable works by creating a constant value that cannot be changed.
public class FinalVariableExample { public static void main(String[] args) { final int DAYS_IN_WEEK = 7; System.out.println("Days in a week: " + DAYS_IN_WEEK); // DAYS_IN_WEEK = 8; // This line would cause a compile error } }
Code Comparison: Final Method
This example shows a final method that cannot be overridden by subclasses.
class Parent { public final void show() { System.out.println("This is a final method."); } } class Child extends Parent { // Trying to override show() will cause a compile error // public void show() { // System.out.println("Override attempt."); // } } public class FinalMethodExample { public static void main(String[] args) { Child c = new Child(); c.show(); } }
Code Comparison: Final Class
This example shows a final class that cannot be extended by any other class.
final class Utility { public static void printMessage() { System.out.println("Utility class method."); } } // The following subclass will cause a compile error // class ExtendedUtility extends Utility { // } public class FinalClassExample { public static void main(String[] args) { Utility.printMessage(); } }
When to Use Which
Choose final variable when you want to create constants that should never change after assignment, such as configuration values or fixed numbers.
Choose final method when you want to lock down a method's behavior so subclasses cannot change it, ensuring consistent and secure logic.
Choose final class when you want to prevent any class from inheriting your class, often for security, immutability, or design reasons like utility classes.