title: How FileInputStream instance is automatically created when Closeable interface is implemented by some custom class when using try-with-resources
body: <p>I have been working with try-with-resources statement.</p>
  <pre><code>try(FileReader rd = new FileReader(&quot;Test.txt&quot;);){} 
  catch (Exception e) {e.printStackTrace();}
  </code></pre>
  <p>The benefit of using try with resources is mainly to do with avoiding to specify the finally block to close out resources.</p>
  <p>Thats where my research process kicked in.</p>
  <p>After doing some debugging, I found out the
  <strong>FileReader extends InputStreamReader</strong>. Inside
  FileReader class this constructor called</p>
  <pre><code>public FileReader(File file) throws FileNotFoundException {
    super(new FileInputStream(file));
  }
  </code></pre>
  <p>which creates an object of FileInputStream class. <strong>FileInputStream extends InputStream which implements Closeable interface</strong>.</p>
  <p>Inside FileInputStream class close method is being called as below and doing what it needs to do to close out resources using native method.</p>
  <pre><code>public void close() throws IOException {
          synchronized (closeLock) {
              if (closed) {
                  return;
              }
              closed = true;
          }
          if (channel != null) {
             channel.close();
          }
  
          fd.closeAll(new Closeable() {
              public void close() throws IOException {
                 close0();
             }
          });
      }
  </code></pre>
  <p>So I understood this is how the close method is being called.</p>
  <p>Now what I fail to understand when I implement the Closeable interface with some custom class
  directly such as</p>
  <pre><code>public class MyClass implements Closeable 
      {
          public void close() 
         {
           System.out.println(&quot;connection closed...&quot;);
         }
      }
  </code></pre>
  <p>and use it like so</p>
  <pre><code>try(MyClass rd = new MyClass();)
  {} 
  catch (Exception e) 
  {e.printStackTrace();}
  </code></pre>
  <p>It is still calling the the close method in custom class <strong>MyClass</strong> automatically without me calling it explicitly.
  When I ran through debug it is going into FileInputStream class which extends InputStream  class which implements Closeable interface. And then finally this method is being called</p>
  <pre><code>public void close() throws IOException {
          synchronized (closeLock) {
              if (closed) {
                  return;
              }
              closed = true;
          }
          if (channel != null) {
             channel.close();
          }
  
          fd.closeAll(new Closeable() {
              public void close() throws IOException {
                 close0();
             }
          });
      }
  </code></pre>
  <p>Can someone please explain to me how FileInputStream instance/object is being created?</p>
  <p>Thanks in advance.</p>