Skip to content
Justice Tech School

try with resources

Resources that implement AutoCloseable close automatically.

Example
public class Main {
  static class Res implements AutoCloseable {
    public void close() { System.out.println("closed"); }
  }
  public static void main(String[] args) {
    try (var r = new Res()) {
      System.out.println("using");
    }
  }
}

Result

using
closed

Exercise

Auto close.

try ( r = new R())

Quiz Test

What closes the resource?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.