019-007-006

Try-with-Resources Basics

Easy

Problem Description

Try-with-Resources Basics

Learning Objective: Understand how to automatically release resources using try-with-resources statement

Overview

Create a SimpleResource class implementing AutoCloseable and verify it is automatically closed with try-with-resources.

Specifications

  • SimpleResource class: implements AutoCloseable
    • Constructor outputs "Resource created"
    • doWork(): outputs "Working..."
    • close(): outputs "Resource automatically closed"
  • In main, use try-with-resources statement

Output Format

Resource created
Working...
Resource automatically closed

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?