Today I learned about the try-with-resources Statement that was included with Java 7. To quote the docs:
“A resource is an object that must be closed after the program is finished with it.”
“The try-with-resources statement ensures that each resource is closed at the end of the statement”
“Any object that implements java.lang.AutoCloseable…can be used as a resource”
The basic gist here is that, when using a class that implements AutoCloseable / Closeable, we no longer have to include a finally block that closes the resource.
Here’s an example using the Apache CuratorFramework: