Java 7 introduced a new language construct, called the try-with-resources statement, for automatic resource management:
void foo() {
try (open file or resource here) {
// do stuff
}
// after try block, resource will close automatically
}
This works with non-memory resources such as files, streams, sockets, and database connections.