Creating and Destroying Objects in Java

All classes extend the Object class, and thus inherit methods from that class.


Example:
public class MyFile {
    private Stream File;

    public MyFile(String path) {
        File = new Stream(path);
    }

    // ...

    public void close() {
        if (File != null) {
            File.close();
            File = null;
        }
    }

    protected void finalize() throws Throwable {
       super.finalize();
       close();
    }
}