0Pricing
Java Academy · Lesson

try-with-resources (AutoCloseable)

Use try-with-resources and AutoCloseable to manage resources safely.

try-with-resources (AutoCloseable) is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Resources like files or sockets need closing. try-with-resources closes them automatically if they implement AutoCloseable.

AutoCloseable concept

Any class that implements AutoCloseable can be used. Common ones include FileReader, BufferedReader, and JDBC connections.

Code: FileReader auto-close

This FileReader is closed automatically after the try block ends, even if an exception occurs.

public class Main {
  public static void main(String[] args) {
    try (java.io.FileReader fr = new java.io.FileReader("test.txt")) {
      int ch = fr.read();
      System.out.println("First char code=" + ch);
    } catch (java.io.IOException e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
}

Multiple resources

You can open multiple resources in one try. They will be closed in reverse order automatically.

Code: two resources

Both BufferedReader and PrintWriter are closed automatically, in reverse order of creation.

public class Main {
  public static void main(String[] args) {
    try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader("test.txt"));
         java.io.PrintWriter pw = new java.io.PrintWriter("out.txt")) {
      String line = br.readLine();
      pw.println(line);
    } catch (java.io.IOException e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
}

Code: custom AutoCloseable

You can implement AutoCloseable yourself. The close() method will be called automatically.

public class Main {
  static class MyRes implements AutoCloseable {
    public void use() { System.out.println("Using resource"); }
    public void close() { System.out.println("Closed resource"); }
  }
  public static void main(String[] args) {
    try (MyRes r = new MyRes()) {
      r.use();
    }
  }
}

try-with-resources check

Quick check: What is the main benefit of try-with-resources?

Recap

Recap: try-with-resources works with AutoCloseable. It ensures safe cleanup of files, streams, and custom resources.

Frequently asked questions

Is the “try-with-resources (AutoCloseable)” lesson free?

Yes — the full text of “try-with-resources (AutoCloseable)” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “try-with-resources (AutoCloseable)”?

Use try-with-resources and AutoCloseable to manage resources safely. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “try-with-resources (AutoCloseable)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Throwing & Custom Exceptions
  2. try-with-resources (AutoCloseable)
  3. Defensive Programming
← Back to Java Academy