On this page
  1. AutoCloseable
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

AutoCloseable

Prev: `try with resources` | Up: builtins | Next: `RuntimeException`
Rel: builtins, Classes, Records & Sealed Hierarchies
Ref: Java Docs — AutoCloseable
Tags: #public #prog #java #builtins #tier/core


Availability

Built-in / java.lang (java.lang Interface) — available with zero import statements.

Syntax & Overview

public interface AutoCloseable { void close() throws Exception; }

Protocol enabling deterministic resource cleanup via try-with-resources.

Notes & Pitfalls

Example

class TempResource implements AutoCloseable {
    boolean closed = false;
    @Override public void close() { closed = true; }
}

TempResource ref;
try (var r = new TempResource()) {
    ref = r;
}
assert ref.closed;

Linked from

Related notes