Simple Try With Resource - Java Language Basics

Java examples for Language Basics:try with Resources

Description

Simple Try With Resource

Demo Code


public class _01SimpleTryWithResource {
  public static void main(String[] args) {
    try (Test test = new Test();) {
      test.show();/*from  w  w w  . j  a  v a2  s  . c  o  m*/
      // No Need to Call it
      // test.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    } finally {
    }
  }

}

class Test implements AutoCloseable {

  @Override
  public void close() throws Exception {
    System.out.println("Connection to this class is automatically closing");
  }

  void show() {
    System.out.println("Show Method is called");
  }
}

Related Tutorials