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

What is autoboxing in java?

598


Mention some features of java?

534


Can a private method of a superclass be declared within a subclass?

525


What is void data type?

568


What is connection class in java?

502






What is use of valueof () in java?

551


What are the classes of java?

531


How can you traverse a linked list in java?

674


Why java uses the concept of the string literal?

541


Can we convert integer to string in java?

589


Explain garbage collection in java?

537


How to add menushortcut to menu item?

557


What is a buffer in java?

567


How many inner classes can a class have?

565


How many digits can a float hold?

528