Sub topic 16
Reusing Existing Variables as Resources
So far, every try-with-resources statement in this tutorial has created its resource inside the parentheses after try. Sometimes you already have the resource in a variable. Maybe another line of code created it, or a method gave it to you. Since Java 9, you can put that existing variable straight into the try parentheses.
The Old Way (Before Java 9)
In Java 7 and 8, the parentheses had to declare a new variable. If you already had a resource, you had to copy it into a second variable:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class OldWay {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader("hello"));
try (BufferedReader r = reader) {
System.out.println(r.readLine());
}
}
}This works, but the extra name r adds nothing. It is just noise.
The Java 9 Way
Now you can write the variable name by itself:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class ReuseDemo {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader("first\nsecond"));
try (reader) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
System.out.println("reader is now closed");
}
}The output is:
first
second
reader is now closedJava still calls reader.close() when the block ends, just as it would for a resource you declared inside the parentheses.
The Rule: Final or Effectively Final
There is one rule. The variable must be final or effectively final.
- A final variable is one marked with the
finalkeyword. It can never be given a new value. - An effectively final variable has no
finalkeyword, but your code never gives it a new value after the first one. Java treats it as if it were final.
This rule makes sure Java closes the same object you opened. If the variable could change, Java might close a different object, or none at all.
The rule also covers fields. A field marked final can be used too, for example try (this.reader).
Several Existing Variables
You can list more than one existing variable, separated by semicolons. You can also mix them with new declarations, as in try (a; Resource b = new Resource("b")). As before, they close in reverse order: the last one listed closes first.
Exercise
Does this code compile? Why or why not? BufferedReader reader = new BufferedReader(new StringReader("x")); reader = new BufferedReader(new StringReader("y")); try (reader) { System.out.println(reader.readLine()); }
Show answer
No. The variable reader is assigned a second time, so it is not effectively final. Java rejects it with a compile error. To fix it, remove the second assignment or use a new variable that is assigned only once.
Exercise
What happens if you call reader.readLine() on the line right after the try (reader) block ends?
Show answer
The reader was closed when the block ended, so readLine() throws an IOException ("Stream closed").