Sub topic 12
AutoCloseable and Closeable
In the last chapter, you closed resources by hand inside a finally block. That works, but Java needs a way to know that an object can be closed. Two interfaces do that job: AutoCloseable and Closeable.
An interface is a promise. A class that implements it promises to provide certain methods. Both of these interfaces promise one method, close().
The AutoCloseable interface
AutoCloseable is in the java.lang package, so you never need to import it. It was added in Java 7. It looks like this:
public interface AutoCloseable {
void close() throws Exception;
}Any class that holds something which must be released can implement it. That could be a file, a network connection, or a database link. The close() method is where the release happens.
The Closeable interface
Closeable is older. It lives in java.io and was made for input and output classes. Since Java 7 it extends AutoCloseable, which means every Closeable is also an AutoCloseable.
public interface Closeable extends AutoCloseable {
void close() throws IOException;
}Classes such as BufferedReader, FileInputStream, and FileWriter implement Closeable.
How they differ
| Feature | AutoCloseable | Closeable |
|---|---|---|
| Package | `java.lang` | `java.io` |
| `close()` may throw | `Exception` | `IOException` only |
| Calling `close()` twice | Should be safe, but not required | Must be safe and do nothing the second time |
| Typical use | Any resource | Input and output streams |
Calling a method a second time with no extra effect is called being idempotent. Closeable requires this. AutoCloseable only recommends it.
Implementing AutoCloseable yourself
Here is a small class that pretends to be a resource. Its close() throws nothing, and it ignores a second call.
public class DoorDemo {
static class Door implements AutoCloseable {
private boolean open = true;
void walkThrough() {
if (!open) {
throw new IllegalStateException("door is closed");
}
System.out.println("walking through");
}
@Override
public void close() {
if (open) {
open = false;
System.out.println("door closed");
}
}
}
public static void main(String[] args) {
Door door = new Door();
try {
door.walkThrough();
} finally {
door.close();
}
door.close(); // second call does nothing
}
}Output:
walking through
door closedThe @Override annotation asks the compiler to check that close() really matches the interface method. The open flag makes the method idempotent.
Right now you still call close() yourself in finally. Because Door implements AutoCloseable, Java can call close() for you instead. The next chapter shows how.
Exercise
You write a class Timer that implements AutoCloseable. Its close() method only prints a message and can never fail. Should the method be declared as public void close() throws Exception? Explain.
Show answer
No. An implementing method may throw fewer exceptions than the interface allows. Declare it as public void close() with no throws clause. Then code that uses Timer does not have to catch or declare Exception.
Exercise
Is every Closeable also an AutoCloseable? Is every AutoCloseable also a Closeable?
Show answer
Every Closeable is an AutoCloseable, because Closeable extends AutoCloseable. The reverse is false. A class can implement AutoCloseable without implementing Closeable, like the Door class in this chapter.