5 さらに簡潔になったtry-with-resources文

finalとしてのリソースがすでにあるか、実質的にfinalの変数がある場合、新しい変数を宣言せずにその変数をtry-with-resources文で使用できます。「事実上final」の変数とは、初期化された後に値が変更されることがない変数のことです。

たとえば、次の2つのリソースを宣言したとします。

        // A final resource
        final Resource resource1 = new Resource("resource1");
        // An effectively final resource
        Resource resource2 = new Resource("resource2");

Java SE 7または8では、次のように新しい変数を宣言します。

        try (Resource r1 = resource1;
             Resource r2 = resource2) {
            ...
        }

Java SE 9では、r1およびr2を宣言する必要はありません。

// New and improved try-with-resources statement in Java SE 9
        try (resource1;
             resource2) {
            ...
        }

the try-with-resources文の詳細な説明は、Javaチュートリアル(Java SE 8以前)を参照してください。