CKTIS / Java
AutoCloseable.close(): automatic resource cleanup
`close()` releases an object's resources. A try-with-resources statement calls it automatically, but the interface does not require a repeated call to have no effect.
Version: Java SE 21
Syntax
void close() throws Exception;
// In a class implementing AutoCloseable, when closing needs no checked exception:
@Override
public void close() { /* release the resource */ }
// With an already initialized final or effectively final resource variable:
try (resource) {
// use the resource
}AutoCloseable.close is the no-argument method of java.lang.AutoCloseable. It returns no value (void): it closes the object and releases the resources it manages. The declaration throws Exception allows a checked exception if closing fails. An override can declare a narrower exception type or no checked exception at all. A manual call through a reference typed as AutoCloseable must catch or declare Exception, even if the actual implementation does not declare it.
A try-with-resources statement calls close() on successfully initialized, non-null resources in reverse initialization order. This happens both on normal exit and when the block throws. If the block and close() throw distinct Throwable instances, the block's exception remains primary. If suppression is enabled on that exception, the closing exception is attached to it and can be retrieved with getSuppressed(). If the block and close() throw the exact same Throwable instance, the implicit call to addSuppressed() attempts self-suppression and throws an IllegalArgumentException, which propagates instead of the original exception. If the block completes normally, a closing exception can propagate.
Here, idempotence means that calling close() after the resource is already closed has no further effect. AutoCloseable recommends this but does not require it. In contrast, java.io.Closeable extends AutoCloseable, requires no effect after closing, and declares IOException instead of Exception. In the example, MemoryBuffer supplies idempotence: the first close clears its array reference, and subsequent calls return immediately. Clearing that reference does not force immediate garbage collection.
Examples
public class Main {
private static final class MemoryBuffer implements AutoCloseable {
private int[] data = {42};
int read() {
if (data == null) {
throw new IllegalStateException("Buffer is closed");
}
return data[0];
}
@Override
public void close() {
if (data == null) {
return;
}
data = null;
System.out.println("Buffer closed");
}
}
public static void main(String[] args) {
MemoryBuffer buffer = new MemoryBuffer();
try (buffer) {
System.out.println("Value: " + buffer.read());
}
buffer.close();
System.out.println("Done");
}
}Result
Value: 42 Buffer closed Done
`buffer` is never reassigned, so it is effectively final and can be used in `try (buffer)`. The automatic close prints `Buffer closed`; the later manual call to `buffer.close()` does nothing.
Related lessons
- Java · Lesson 31 · Intermediate · Version: 2