Why Do We Use finalize Method in Java ?

Java provides finalize() method to perform any cleanup before Garbage Collection. This method is in Object class, and it is invoked by JVM internally. Developers are free to implement this method for any custom cleanup in case of Garbage Collection.

If an Object is not Garbage Collected, then this method may not be called.

This method is never invoked more than once by JVM.



You May Interest

When Do You Use Exception or Error in Java ?

Why String Class is Immutable or Final in Java ?

How Can We Prevent a Deadlock in Java ?

How Can We Improve the Performance of IdentityHashMap in Java ?

Can We Use Thread.sleep Method For Real-time Processing in Java ?