finalize() method?

Answer Posted / ramanareddy333

every class inherits the finalize() method from
java.lang.Object
the method is called by the garbage collector when it
determines no more references to the object exist
the Object finalize method performs no actions but it may
be overridden by any class
normally it should be overridden to clean-up non-Java
resources ie closing a file
if overridding finalize() it is good programming practice
to use a try-catch-finally statement and to always call
super.finalize() (JPL pg 47-48). This is a saftey measure
to ensure you do not inadvertently miss closing a resource
used by the objects calling class
protected void finalize() throws Throwable {
try {
close(); // close open files
} finally {
super.finalize();
}
}

any exception thrown by finalize() during garbage
collection halts the finalization but is otherwise ignored
finalize() is never run more than once on any object

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How would you use Bubble Sort to sort the number of elements?

815


What is a stack class in java ?

847


What is locale in java?

792


What are the types of literals?

806


Can we call the constructor of a class more than once for an object?

832


Where is jre installed?

801


What is difference between length and length() method in java ?

767


What is advantage of java?

765


Why we go for collections in java?

752


What is a website container?

693


What is java used for?

775


What are the four corner stones of oop?

782


What is string [] java?

736


How can we find the sum of two linked lists using stack in java?

776


What is the use of arrays tostring () in java?

740