Java 9 Using effectively-final variables in the try-with-resources
In Java 9 try-with-resources statement has been improved. Now if you already have final or effectively final resource you can use it in try-with-resources statement without need to declare a dummy variable.
Effectively final is the variable whose value is never changed after assignment.
For example in before Java 9 way of using the try-with-resources statement would be:
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"));) {
while (br.readLine() != null) {
System.out.println(br.readLine());
}
}
In Java 9 the same code could be rewritten like this:
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try (br) {
while (br.readLine() != null) {
System.out.println(br.readLine());
}
}