Final vs Finally vs Finalize in Java: Key Differences and Usage
final is a keyword used to declare constants, prevent method overriding, or inheritance. finally is a block used to execute important code after try-catch, regardless of exceptions. finalize is a method called by the garbage collector before object destruction, now deprecated since Java 9.Quick Comparison
Here is a quick table summarizing the key differences between final, finally, and finalize in Java.
| Aspect | final | finally | finalize |
|---|---|---|---|
| Type | Keyword | Block | Method |
| Purpose | Declare constants, prevent inheritance/overriding | Execute code after try-catch | Cleanup before garbage collection |
| Usage Location | Variables, methods, classes | Try-catch blocks | In a class, overridden from Object |
| Execution Time | Compile-time effect | Runs after try-catch at runtime | Runs before object is garbage collected |
| Exception Handling | No | Yes, always executes even if exception occurs | No |
| Status | Active and widely used | Active and widely used | Deprecated since Java 9 |
Key Differences
final is a keyword that you use to make a variable's value unchangeable, prevent a method from being overridden, or stop a class from being subclassed. For example, a final variable acts like a constant.
finally is a block that always runs after a try and catch block, no matter what happens. It is used to clean up resources like closing files or database connections.
finalize is a method that the Java garbage collector calls before destroying an object. It was meant to let you release resources, but it is unreliable and deprecated since Java 9. You should avoid using it and use other cleanup methods instead.
Code Comparison
Example showing final usage to declare a constant and prevent method overriding.
public class FinalExample { final int constantValue = 10; // constant variable final void show() { System.out.println("This method cannot be overridden."); } public static void main(String[] args) { FinalExample obj = new FinalExample(); System.out.println("Constant value: " + obj.constantValue); obj.show(); } }
finally Equivalent
Example showing finally block used to close a resource after try-catch.
public class FinallyExample { public static void main(String[] args) { try { int result = 10 / 0; // causes ArithmeticException } catch (ArithmeticException e) { System.out.println("Exception caught: " + e.getMessage()); } finally { System.out.println("This finally block always executes."); } } }
When to Use Which
Choose final when you want to create constants or prevent changes to classes or methods. Use finally to ensure important cleanup code runs after try-catch blocks, like closing files or releasing resources. Avoid using finalize because it is deprecated and unreliable; instead, use try-with-resources or explicit cleanup methods.
Key Takeaways
final is for constants and preventing inheritance or overriding.finally block always runs after try-catch for cleanup.finalize is a deprecated method for cleanup before garbage collection.finalize; prefer explicit resource management.final and finally appropriately to write safer Java code.