public interface DisposableA resource that can be disposed when waiting for the garbage collector would be overly conservative. Invoking the
dispose()method allows any resources held by this object to be released. The result of calling any other method subsequent to a call to this method is undefined.Relationship withSome classes may implement both the
Closeableinterfaces. While very similar, those two interfaces serve slightly different purposes. The
Closeableinterface closes a stream or a connection, but some classes allow the object to be reused with a different stream. However once an object has been disposed, it can not be used anymore.Example:Another difference is that
ImageWriterallow to reuse the same instance many times for reading or writing different images in the same format. New streams can be created, given to the
ImageWriterand closed many times as long as
dispose()has not been invoked.
dispose()does not throw any checked exception. That method may be invoked in a background thread performing cleanup tasks, which would not know what to do in case of failure. Error during
dispose()execution should not result in any lost of data.