Tuesday, December 13, 2011

How is final different from finally and finalize()?

final is a modifier which can be applied to a class or a method or a variable. If a class is declared  as final, it can't be inherited, If a method is declared as final, it can't be overridden and if a  variable is declared as final , it  can't be changed.

finally keyword is used in exception handling mechanism. It will be executed after completion of try/catch block. The main difference between catch and finally is, catch() block will be executed only if there is any exception in try block. But finally block is executed even there is no exception.

finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.