Section 11.12 try-with-Resources: Automatic Resource Deallocation
• The try
-with-resources statement (p. 467) simplifies writing code in which you obtain a resource, use it in a try
block and release the resource in a corresponding finally
block. Instead, you allocate the resource in the parentheses following the try
keyword and use the resource in the try
block; then the statement implicitly calls the resource’s close
method at the end of the try
block.
• Each resource must be an object of a class that implements the AutoCloseable
interface (p. 467)—such a class has a close
method.
• You can allocate multiple resources in the parentheses following try
by separating them with a semicolon (;
).
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.