The counterpart of the constructor a finalizer is called automatically before a class is garbage collected. This can be used to explicitly release any resources tied up by the object. To create a finalizer, simply override the method finalize as defined in the Object class.
In object-oriented programming languages that use garbage collection, a finalizer is a special method that is executed when an object is garbage collected. It is similar in function to a destructor. In less technical terms, a finalizer is a piece of code that ensures that certain necessary actions are taken when an acquired resource (such as a file or access to a hardware device) is no longer being used.