CrazyEngineers
  • I know that the final keyword can be used for classes, methods and for variables. And If I am not wrong the final class can not be subclassed and the final method can not be overridden and the fianl variable can not be changed from its initialized value. What exactly Finalize() method in a Java program do? When can we use that method? Can someone please explain with an example program?
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • monujatt

    MemberAug 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;
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberAug 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register