Finalizer.java

Download Finalizer.java

 1: // Demo of Finalizers.  Not a complete program!
 2: // Written 2/2001 by Wayne Pollock, Tampa Florida USA
 3: 
 4: public class DBFile
 5: {
 6:    private FileReader file;
 7:    public DBFile ( String fileName ) throws FileNotFoundException
 8:    {
 9:      file = new FileReader( fileName );
10:    }
11: 
12:    public synchronized void close () throws IOException
13:    {
14:       if ( file != null )
15:       {  file.close();
16:          file = null;
17:       }
18:    }
19: 
20:    protected void finalize () throws Throwable
21:    {  try
22:       {  close();
23:       }
24:       finally
25:       {  super.finalize();  // Always use this.
26:       }
27:    }
28: }