The java Final keyword can be used in many context.
- Variable - If you make any variable as final, you cannot change the value of final variable(It will be constant).
- Method - If you make any method as final, you cannot override it.
- Class - If you make any class as final, you cannot extend it. read more
The finally Block
The finally
block always executes when the try
block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. Putting cleanup code in a finally
block is always a good practice, even when no exceptions are anticipated. read more
finalize method in java is a special method much like main method in java. finalize()
is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity
Main issue with finalize
method in java is its not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example an object may wait indefinitely after becoming eligible for garbage collection and before its finalize()
method gets called. similarly even after finalize gets called its not guaranteed it will be immediately collected. read more