問題一覧へ 例外処理 標準

try-with-resources のクローズ順序

次のコードを実行すると何が表示されますか。

public class Main {
  static class Res implements AutoCloseable {
    String n;
    Res(String n) { this.n = n; System.out.print("open:" + n + " "); }
    public void close() { System.out.print("close:" + n + " "); }
  }

  public static void main(String[] args) {
    try (Res a = new Res("A"); Res b = new Res("B")) {
      System.out.print("use ");
    }
  }
}

選択肢