0
0
JavaComparisonBeginner · 4 min read

Final vs Static in Java: Key Differences and Usage

In Java, final means a variable, method, or class cannot be changed or overridden after assignment or declaration, ensuring immutability. static means a member belongs to the class itself rather than any instance, allowing shared access across all objects.
⚖️

Quick Comparison

Here is a quick table showing the main differences between final and static keywords in Java.

Aspectfinalstatic
PurposePrevents reassignment or modificationBelongs to the class, shared by all instances
UsageVariables, methods, classesVariables, methods, blocks
Effect on VariablesValue cannot change after initializationSingle copy shared by all objects
Effect on MethodsCannot be overriddenCan be called without object
Effect on ClassesCannot be subclassedNot applicable
MemoryEach object can have its own final variableOnly one copy in memory
⚖️

Key Differences

The final keyword in Java is used to make variables constant, methods un-overridable, and classes un-inheritable. When a variable is declared final, its value must be assigned once and cannot be changed later. For methods, final prevents subclasses from changing the method's behavior. For classes, final means no other class can extend it.

On the other hand, static means the member belongs to the class itself, not to any specific object. A static variable is shared among all instances of the class, so changes affect all objects. A static method can be called without creating an object, using the class name directly. static cannot be applied to classes to prevent inheritance.

In summary, final is about immutability and preventing change, while static is about sharing and belonging to the class rather than instances.

⚖️

Code Comparison

Here is an example showing how final is used to create a constant variable and prevent method overriding.

java
class FinalExample {
    final int number = 10; // final variable

    final void show() { // final method
        System.out.println("Number is " + number);
    }
}

class Child extends FinalExample {
    // Trying to override final method will cause error
    // void show() { System.out.println("Override"); } // Error
}

public class Main {
    public static void main(String[] args) {
        FinalExample obj = new FinalExample();
        obj.show();
    }
}
Output
Number is 10
↔️

Static Equivalent

This example shows how static variables and methods belong to the class and can be accessed without creating objects.

java
class StaticExample {
    static int count = 0; // static variable

    static void displayCount() { // static method
        System.out.println("Count is " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        StaticExample.count = 5; // Access without object
        StaticExample.displayCount();
    }
}
Output
Count is 5
🎯

When to Use Which

Choose final when you want to make sure a variable's value never changes, a method cannot be overridden, or a class cannot be extended. This is useful for constants and ensuring behavior stays consistent.

Choose static when you want to share a variable or method across all instances of a class or when you want to call a method without creating an object. This is useful for utility methods or shared data.

Key Takeaways

final prevents change; static shares across all instances.
final can apply to variables, methods, and classes; static applies to variables and methods only.
static members belong to the class, final members ensure immutability or no override.
Use final for constants and fixed behavior, static for shared data or utility methods.
Trying to override a final method or change a final variable causes compile errors.