There are various other components of a class that affect it functioning actively or passively. These are described below.
Garbage Collection
Since objects are dynamically allocated by using the new operator , java handles the deallocation automatically. The technique used is called as Garbage Collection.
In this , when no reference to an object exist , that object is assumed to be no longer needed , and the memory occupied by the object can be reclaimed.
The garbage collection occurs sporadically during the execution of the execution of the program. It won't occur simply because 1 or more objects exist that are no longer used.
The finalize ( ) method
Sometimes an object will need to perform some action when it is destroyed. If an object is holding some non-java resource such as file handler or window character font , then you might want to make sure these resources are freed before an object is destroyed.
To handle such situations , java provide a mechanism called finalization. Through this , you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.
To add finalizer , we define finalize ( ) method.
syntax:
protected void finalize ( )
{
//finalization code
}
No comments:
Post a Comment