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
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.