2. finalize() method.
• Sometime you will need to do some actions when
an object is destroyed by garbage collection.
• For example when your program is using some
non-java resource, you have to free the memory
for those resources.
• This process is knows as finalization.
• The finalization is done by the finalize() method.
The finalize() method like:
3. Protected void finalize()
{
//statement to be executed when finalization occurs….
}
• Finalize method done not return any value so the return
type is void.
• Protected keyword is access-specifier so that any object of
outside class can not access to this method.
• The finalize() method is called just before when java
runtime system performs garbage collection.
• In C++ you can define a destructor for a class to destroy an
object when it goes out of scope. In java there is no need
for this.