Try With Resources Pattern
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 62 of 87.
When working with resources like file streams or database connections, you must always close them when finished—otherwise you risk memory leaks. The traditional approach uses a finally block, but it's verbose and error-prone.
Java's try-with-resources statement automatically closes resources when the block completes. Any class that implements the AutoCloseable interface can be used with this syntax:
// Traditional approach - verbose and risky
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("file.txt"));
String line = reader.readLine();
} finally {
if (reader != null) {
reader.close(); // Could also throw an exception!
}
}
// Try-with-resources - clean and safe
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
String line = reader.readLine();
} // Automatically closed hereYou can manage multiple resources by separating them with semicolons. They're closed in reverse order of declaration:
try (FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr)) {
// Use both resources
} // br closed first, then frTo create your own auto-closeable class, implement the AutoCloseable interface and its single close() method:
public class DatabaseConnection implements AutoCloseable {
public DatabaseConnection() {
System.out.println("Connection opened");
}
@Override
public void close() {
System.out.println("Connection closed");
}
}This pattern ensures resources are always properly released, even if exceptions occur during processing.
Challenge
EasyLet's build a resource management system that demonstrates the power of try-with-resources! You'll create custom auto-closeable classes that simulate managing connections and sessions, ensuring they're always properly cleaned up.
You'll organize your code across three files:
Connection.java: Create a class that simulates a database connection. YourConnectionclass should implementAutoCloseable. It needs a private field for the connection name (String), a constructor that takes the name and prints[name] connection opened, aquery(String sql)method that printsExecuting on [name]: [sql], and the requiredclose()method that prints[name] connection closed.Session.java: Create another auto-closeable class that simulates a user session. YourSessionclass should also implementAutoCloseable. Include a private field for the username (String), a constructor that takes the username and printsSession started for [username], aperformAction(String action)method that prints[username] performed: [action], and aclose()method that printsSession ended for [username].Main.java: Bring your resource management system together! You'll receive three inputs: a connection name, a username, and an action to perform.First, demonstrate single resource management. Print
=== Single Resource ===, then use try-with-resources to create aConnectionwith the given name. Inside the try block, callquery("SELECT * FROM users"). The connection should automatically close when the block ends.Next, demonstrate multiple resources. Print a blank line, then
=== Multiple Resources ===. Use try-with-resources with both aConnection(using the same name) and aSession(using the username), declared in that order with semicolons. Inside the try block, first callquery("INSERT INTO logs")on the connection, then callperformActionwith the input action on the session. Watch how resources close in reverse order!
You will receive three inputs in order: connection name (String), username (String), and action (String).
Notice how try-with-resources eliminates the need for explicit close calls and finally blocks. The resources are guaranteed to close even if exceptions occur, and multiple resources close in reverse declaration order!
Cheat sheet
The try-with-resources statement automatically closes resources when the block completes. Any class implementing AutoCloseable can be used:
try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
String line = reader.readLine();
} // Automatically closed hereMultiple resources can be managed by separating them with semicolons. They close in reverse order of declaration:
try (FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr)) {
// Use both resources
} // br closed first, then frTo create a custom auto-closeable class, implement the AutoCloseable interface and its close() method:
public class DatabaseConnection implements AutoCloseable {
public DatabaseConnection() {
System.out.println("Connection opened");
}
@Override
public void close() {
System.out.println("Connection closed");
}
}Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String connectionName = scanner.nextLine();
String username = scanner.nextLine();
String action = scanner.nextLine();
// === Single Resource ===
// TODO: Print the header
// TODO: Use try-with-resources to create a Connection
// TODO: Inside the try block, call query("SELECT * FROM users")
// === Multiple Resources ===
// TODO: Print a blank line and the header
// TODO: Use try-with-resources with both Connection and Session
// TODO: Inside the try block:
// - Call query("INSERT INTO logs") on the connection
// - Call performAction with the input action on the session
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting10Exception Handling in OOP
Exception Class HierarchyCustom ExceptionsChecked vs Unchecked ErrorsTry With Resources PatternRecap - Validated User2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System