Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@monujatt-1M9EHi • Aug 5, 2012
Finalize() method is basically used for the garbage collection (for releasing resources like opened files etc.)
class TestFile {
FileInputStream aFile = null;
TestFile(String filename) {
try {
aFile = new FileInputStream(filename);
} catch (java.io.FileNotFoundException e) {
System.err.println("Could not open file " + filename);
}
}
}
protected void finalize () throws throwable {
if (aFile != null) {
aFile.close();
aFile = null;
}
} -
@sulochana-anand-OrGmKr • Aug 7, 2012
It can be used when an object is about to destroy bu need to perform some action.there might be some situation where an object holding non java resouce such as file handler or charecter font etc and a programar want to make sure these resouces are freed before an object is destroyed.in such situation java provide us a method finalize().by using this we can define an action that is about to occur when an object is about to regaind by garbage collector.