Method
Java AutoCloseable.close()
Releases resources according to the implementation contract.
Formal Syntax & Structure
java syntaxjava
void close() throws ExceptionWorking Example
Example Snippetjava
class Lease implements AutoCloseable { private final String name; Lease(String name) { this.name = name; } void use() { System.out.println("Use " + name); } public void close() { System.out.println("Close " + name); }}
public class Main { public static void main(String[] args) { try (Lease lease = new Lease("A")) { lease.use(); } System.out.println("Done"); }}