AutoCloseable

An object that may hold resources (such as file or socket handles) until it is closed. The {@link #close()} method of an {@code AutoCloseable} object is called automatically when exiting a {@code try}-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.

@apiNote <p>It is possible, and in fact common, for a base class to implement AutoCloseable even though not all of its subclasses or instances will hold releasable resources. For code that must operate in complete generality, or when it is known that the {@code AutoCloseable} instance requires resource release, it is recommended to use {@code try}-with-resources constructions. However, when using facilities such as {@link java.util.stream.Stream} that support both I/O-based and non-I/O-based forms, {@code try}-with-resources blocks are in general unnecessary when using non-I/O-based forms.

@author Josh Bloch

interface AutoCloseable {}

Members

Functions

close
void close()

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the {@code try}-with-resources statement.

Meta