Sobes.tech
Middle

What interface allows adding an object as a resource in a try block?

sobes.tech AI

Answer from AI

AutoCloseable interface. An object of a class implementing this interface can be used in a try-with-resources statement.

try (ResourceType resource = new ResourceType()) {
    // Work with the resource
} catch (Exception e) {
    // Handle exception
}

When exiting the try block (either normally or due to an exception), the close() method of the resource is automatically called, releasing it.