O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

To demonstrate how the new statement works, let's create our own resources that implement AutoCloseable and use them in a fashion similar to the resources of the previous examples.

Here is one resource:

class MyResource1 implements AutoCloseable {  public MyResource1(){    System.out.println("MyResource1 is acquired");  }  public void close() throws Exception {    //Do what has to be done to release this resource    System.out.println("MyResource1 is closed");  }}

Here is the second resource:

class MyResource2 implements AutoCloseable {  public MyResource2(){    System.out.println("MyResource2 is acquired");  }  public void close() throws Exception {    //Do what has to be done to release this resource    System.out.println("MyResource2 is closed" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required