Java try with resources

In this post, we will see about Java try with resources Statement.

Java try with resources is a feature of Java which was added into Java 7. It helps to close all the resources declared within try block. It automatically closes the resources after being used. A resource can be any file or a database connection.
For example, if we are working with File handing then after opening a file, it requires to be closed after reading or writing data. In early versions of Java, we have to explicitly close the resource, but from Java 7, we don’t need to worry about to close the resources.

What type of resources can be handled automatically?

Java says, any object that implements java.lang.AutoCloseable interface and java.io.Closeable interface, can be used as a resource.

Let’s start with examples to understand the topic.

Older approach to close the resources

This is an older approach that we used to handle file related operations.
Notice, we manually closed the resource using close() method.

Output:

File is written

Java 7 try with resources

The try with resources was introduced with Java 7 and helps to reduce the effort to close the resources manually. Now, we don’ need to use close() method. See the example.

Output:

File is written

Syntax

The following is the syntax of try with resources that includes function like parenthesis to collect resources.

Note: Prior to Java 7 version, only finally block is used to ensure that a resource is closed properly.

Example

Let’s take one more example of Java try with resources. Here, we are creating database connection and not closing the connection because it will be handled by new try block.
Notice, we used multiple resources inside it that means it can handle multiple resources as well.

Java 9 Try with resources Improvements

In Java 9, try with resources is improved that allows to declare or create the resources outside the try block. Before Java 9, it was not possible, and compiler generates error.

Output:

File is written

See, we declared the resource outside the try with resources and compiler compiles the file successfully, but in case of Java 7 or Java 8, we can not do this.

Finally block with try with resources

Although Java try with resources does resource cleanup tasks automatically but in case, if there is any resource which does not implement AutoCloseable interface need to be closed in finally block.
In this example, we are using finally block with try with resources.

Output:

File is written
Finally block is executed

Create Custom AutoCloseable Code

Any class that implements AutoCloseable interface can be used in try with resources. Let’s create a class and implements AutoCloseable interface. This class has some utility methods.
Notice, we did not call close() method of SetupConnection class, but it is called implicitly by the compiler. See the example and its output.

Output:

Connection configured.
Connected
Connection is closed.

That’s all about Java Try with resources.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *