Tuesday, 11 July 2017

Difference between final, finally, and finalize in Java

This is one of the very common question, which is often asked in any Java interview. So, I have given the summarized points on final, finally, and finalize. Hope this will be helpful to you.

final:
  • final is a modifier applicable for classes, methods and variables. If a class declared as final then we cannot extends that class i.e. we cannot create child class of that class.
  • If a method is declared as final then we cannot override that method in the child class.
  • If a variable is declared as final then it will become constant and we cannot perform reassignment for that variable.
finally:
finally is a block always associated with try catch to maintain cleanup code.
finalize( ):
finalize( ) is a method which is always invoked by garbage collector just before destroying an object to perform cleanup activities.

finally is meant for cleanup activities related to try block, whereas finalize( ) is meant for cleanup activities related to object.

If you like the post, please comment, share the post and do like me on Facebook.

Thanks & Regards,
Susanto Paul