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

Write a program to search a number in the given list of numbers.

631


What is the difference between class forname and new?

552


Can a constructor have different name than a class name in java?

594


Why map is used in java?

580


What is string substring?

563






What is a heavyweight component?

542


how to deploy apache tomcat server to weblogic server in java

1599


What is the difference between checked exception and unchecked exception?

530


Why main method is static in java?

596


Explain the public class modifier?

506


What is default constructors?

543


What is the static field modifier?

598


How many bytes is a char in java?

531


Why strings in java are called as immutable?

591


What is byte value?

557