Cognizant Interview Question

Explain the final, finally, and finalize keywords in Java

Interview Answer

Anonymous

May 3, 2024

final keyword: In Java, the final keyword is used to apply restrictions on classes, methods, and variables. When applied to a class, it indicates that the class cannot be subclassed, i.e., it cannot be extended by any other class. finally block: The finally block is used in exception handling along with the try and catch blocks. The code within the finally block is always executed, regardless of whether an exception occurs or not. finalize() method: The finalize() method is a method of the Object class in Java. It is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. The purpose of the finalize() method is to perform any necessary cleanup or releasing of resources associated with the object before it is garbage collected.