All objects in Java implicitly inherit from java.lang.Object
. This class contains multiple methods, which can be overridden in the child class.
One such overridable method is finalize()
. Before objects get garbage collected from memory, the JVM will execute the finalize()
. So, if you need to close any resources that were opened in the class (like backend connections, Files…), it can be done in this method.