What is the difference between final, finally and finalize in Java?
What is the difference between final, finally and finalize in Java?
final – final is a keyword in Java. The variable declared as final cannot be changed after initialization, the method declared as final cannot be overridden, the class declared as final cannot be inherited.
finally – finally is a block in Java which is used along with try-catch 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. Java 7 has a new try with resources statement that you can use to automatically close resources that explicitly or implicitly implement java.io.Closeable or java.lang.AutoCloseable
finalize – finalize is a method in java.lang.Object class. finalize() method is called by the garbage collector before reclaiming the memory allocated to the object. A class overrides the finalize() method to dispose of system resources or to perform other cleanup. However the usage of finalize() method is not recommended because the execution of finalize() method is not guaranteed and there is a severe impact on performance.