Difference between final, finally and finalize in Java
Final is the keyword, Finally is block and Finalize is method. Here we discuss some concept related to Difference between final, finally and finalize in Java
Final keyword
It is used in the following three cases:
If the final keyword is attached to a variable then the variable becomes constant i.e. its value cannot be changed in the program.
If a method is marked as final then the method cannot be overridden by any other method.
If a class is marked as final then this class cannot be inherited by any other class.
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. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Finalize Method
The finalize method is called when an object is about to get garbage collected. That can be at any time after it has become eligible for garbage collection.
No comments :
Post a Comment