[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finalize()



So Bloch says its a mistake to use finalize to manage system resources, but its a good idea to use it as a "safety net", which is what I guess you are advocating. It occurs to me that java.io.RandomAcccesFile probably closes itself in the finalizer, just like FileInputStream. I wonder why linux behaves differently?
Bob Simons wrote:
I looked in the source code for the java.io.FileInputStream (as an example) to see if Java's file classes use the finalize() method. They do.

    /**
* Ensures that the <code>close</code> method of this file input stream is
     * called when there are no more references to it.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FileInputStream#close()
     */
    protected void finalize() throws IOException {
    if (fd != null) {
        if (fd != fd.in) {
        close();
        }
    }
    }

So I don't yet know what Josh Bloch's book says specifically, but Sun uses the finalize method in the actual Java code.

Sincerely,

Bob Simons
Satellite Data Product Manager
Environmental Research Division
NOAA Southwest Fisheries Science Center
1352 Lighthouse Ave
Pacific Grove, CA 93950-2079
(831)648-0272
address@hidden
<>< <>< <>< <>< <>< <>< <>< <>< <><