Final vs Static in Java: Key Differences and Usage
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.
| Aspect | final | static |
|---|---|---|
| Purpose | Prevents reassignment or modification | Belongs to the class, shared by all instances |
| Usage | Variables, methods, classes | Variables, methods, blocks |
| Effect on Variables | Value cannot change after initialization | Single copy shared by all objects |
| Effect on Methods | Cannot be overridden | Can be called without object |
| Effect on Classes | Cannot be subclassed | Not applicable |
| Memory | Each object can have its own final variable | Only 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.
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(); } }
Static Equivalent
This example shows how static variables and methods belong to the class and can be accessed without creating objects.
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(); } }
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.final for constants and fixed behavior, static for shared data or utility methods.final method or change a final variable causes compile errors.