Every class inherits finalize() method from Object class and method is usually called by the garbage collector when ensures that no more references to the object exist .object class finalize method performs no significant action so normally it is overridden by a java class for clean up code e.g. close a file, closing database connections etc.

But best practice is to use a try-catch-finally statement and to always call super.finalize() method. Finalize runs once and only once for every object.

protected void finalize() throws Throwable {
    
    try {
        
        close();
        
    catch(Exception e) {
        
    }
    
    finally {
        
        super.finalize();
        //more code can be written here as per need of application
        
    }
}