0
0
JavaComparisonBeginner · 4 min read

Final Variable vs Final Method vs Final Class in Java: Key Differences

In Java, a 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.

AspectFinal VariableFinal MethodFinal Class
PurposeMake variable value constantPrevent method overridingPrevent class inheritance
ModificationValue cannot be changed after initializationMethod implementation cannot be changed in subclassClass cannot have subclasses
Usage LevelVariableMethodClass
Inheritance ImpactNo effect on inheritanceRestricts method override in subclassesBlocks subclass creation
Example Use CaseConstants like PISecurity-sensitive methodsImmutable 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.

java
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
    }
}
Output
Days in a week: 7
⚖️

Code Comparison: Final Method

This example shows a final method that cannot be overridden by subclasses.

java
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();
    }
}
Output
This is a final method.
⚖️

Code Comparison: Final Class

This example shows a final class that cannot be extended by any other class.

java
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();
    }
}
Output
Utility class method.
🎯

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.

Key Takeaways

Use final variables to create constants that cannot be reassigned.
Use final methods to prevent subclasses from changing important method behavior.
Use final classes to stop inheritance and protect class design.
Each final keyword usage restricts modification at a different level: variable, method, or class.
Choosing the right final usage improves code safety and design clarity.