Javatpoint Logo
Javatpoint Logo

AutoCloseable Interface in Java

Java, being an object-oriented programming language, places a strong emphasis on resource management. One critical aspect of this is ensuring that resources like file handles, database connections, and network connections are properly released when they are no longer needed. The AutoCloseable interface plays a pivotal role in this process.

What is the AutoCloseable Interface?

The AutoCloseable interface is part of the Java language since version 7. It belongs to the java.lang package and is defined as follows:

As we can see, it is a simple interface with a single method, close() that takes no arguments and returns nothing (void). The close() method is designed to release resources held by an object that implements this interface.

The try-with-resources Statement

Java introduced the try-with-resources statement in Java 7, which simplifies the process of managing resources that must be closed. The statement allows us to declare one or more resources within the parentheses of a try statement. These resources are automatically closed after the try block is executed.

Here is an example of using try-with-resources with a FileInputStream, which implements the AutoCloseable interface:

In this example, the FileInputStream fis is declared within the try block. It will be automatically closed after the block, even if an exception occurs.

Implementing AutoCloseable Interface

To make a class compatible with the try-with-resources statement, it needs to implement the AutoCloseable interface. This involves providing an appropriate close() method that releases the resources associated with the class.

For instance, let's say we have a custom Resource class:

In this example, the Resource class implements the AutoCloseable interface and provides an implementation for the close() method.

Now, we can use this class with the try-with-resources statement:

Exception Handling in AutoCloseable

The close() method of an AutoCloseable can throw exceptions. In the example above, the close() method is declared to throw an Exception. It is because it's good practice to handle exceptions that may occur during the closing process.

If the close() method throws an exception, it will not override or suppress any exception that might be thrown from the try block.

Below is a complete Java program that demonstrates the usage of the AutoCloseable interface along with the try-with-resources statement.


Doing something with the resource.
Resource is being closed.

The AutoCloseable interface, along with the try-with-resources statement, provides a convenient and effective way to manage resources in Java. By implementing this interface and utilizing the try-with-resources statement, we can ensure that resources are properly released, even in the presence of exceptions. Remember, it's essential to implement the close() method correctly to release any resources held by your class. It will contribute to more robust and reliable Java programs.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA