What is the difference between final, finally, and finalize() in Java?
1. final
-
Modifier for variables, methods, and classes.
-
Variable: Cannot be reassigned.
-
Method: Cannot be overridden.
-
Class: Cannot be extended.
2. finally
-
A block used with try-catch.
-
Ensures cleanup code (like closing resources) always executes, regardless of exceptions.
3. finalize()
-
A method from Object class, called by the garbage collector before an object is destroyed.
-
Deprecated in Java 9 due to unpredictable behavior.
In short:
final is for restrictions.
finally is for cleanup.
finalize() is for garbage collection (now obsolete).