java.lang.AutoCloseable is the interface that makes an object usable in a try-with-resources statement. Any class that implements it and overrides close() has that method called automatically when the try block ends, whether the block finishes normally or throws, so files, sockets, database connections and locks are released reliably.
Introduced in Java 7, it replaced hand-written finally blocks that were verbose and often lost the original exception. Modern JDKs use it widely, including ExecutorService since Java 19 and HttpClient since Java 21.
The whole interface is one method:
The signature allows close() to throw any Exception, so implementations can narrow it, throwing a more specific exception or none at all. The contract recommends that close() be safe to call more than once, but unlike Closeable it does not require it.
Declare resources in parentheses after try. Java closes them in reverse order of declaration, and then runs any catch or finally block:
Reverse order matters because later resources often depend on earlier ones, such as a BufferedReader wrapping a FileReader. In this example close() declares no checked exception, so the caller needs no catch.
The old finally pattern had a well-known flaw: if both the body and close() threw, the exception from close() replaced the original one. Try-with-resources keeps the body exception as the primary one and attaches the close failures as suppressed exceptions:
You see the root cause first and still keep the secondary failure for diagnosis through Throwable.getSuppressed().
| Feature | AutoCloseable | Closeable |
|---|---|---|
| Package | java.lang | java.io |
| Since | Java 7 | Java 5 |
close() throws | Exception | IOException |
Idempotent close() | Recommended | Required by the contract |
| Extends | None | AutoCloseable |
Use Closeable for I/O-style resources, and AutoCloseable for anything else, such as a lock wrapper, a transaction scope, or a test fixture. Both work in try-with-resources.
Many types you use daily are AutoCloseable, often through Closeable:
InputStream, OutputStream, Reader, Writer, ScannerConnection, Statement and ResultSetjava.nio.file.Files.lines(...) and Files.newDirectoryStream(...) streamsExecutorService (Java 19 and later), where close() waits for submitted tasks to finishHttpClient (Java 21 and later)The last line prints only after the three tasks are done, because close() on the executor blocks until they complete. On older JDKs you would call shutdown() and awaitTermination() by hand.
Since Java 9 you can pass an effectively final variable to try-with-resources instead of declaring it inside the parentheses:
This helps when the resource is created elsewhere, but be careful not to use it after the block, because it is already closed.
close() idempotent: calling it twice should not throw or double-release.finalize() as a safety net. It was deprecated for removal in Java 18. For leak protection use java.lang.ref.Cleaner, and treat it as a backup, not the primary mechanism.InterruptedException from close(). If you must, restore the interrupt status, since the compiler warns about this for AutoCloseable types.Not for closing resources. Use try-with-resources and keep finally for other cleanup that is not a resource.
Java skips close() for a null resource, so no NullPointerException occurs from the close step itself.
No. Closeable extends AutoCloseable, narrows the exception to IOException, and requires idempotent close(). Prefer Closeable for I/O types.
Yes. It has a single abstract method, so a lambda such as AutoCloseable c = () -> System.out.println("done"); is valid, which is handy for small scoped cleanups in tests.
AutoCloseable gives the language a standard contract for deterministic cleanup. Implement it for anything that holds a resource, use try-with-resources so close() runs in reverse order, rely on suppressed exceptions to keep the original failure visible, and prefer Cleaner over finalize() for any safety net.