Java 7 introduced try-with-resources statements.
The feature arrived in Java SE 7 as part of Project Coin, a group of small language improvements. It lets a program declare one or more resources directly in a try statement, such as a file reader, input stream, or database connection. When execution leaves the try block, Java automatically calls each resource’s close method.
Before Java 7, developers commonly used a finally block to close resources manually. That pattern was verbose and could obscure the original exception, especially when cleanup itself failed. Try-with-resources made cleanup more reliable and readable while preserving the normal try-catch structure.
A resource must implement AutoCloseable; the older Closeable interface extends it. If several resources are declared, they are closed in reverse order. Java 9 later expanded the syntax so an already-declared effectively final resource could be referenced in the try header. That enhancement did not change the original answer: the statement form itself debuted in Java 7.