title: Why declare Resource in try() while using try-with-resources
body: <p><strong>The try-with-resources Statement</strong>
  Following is example from Java <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html" rel="nofollow">Docs</a></p>
  
  <pre><code>static String readFirstLineFromFile(String path) throws IOException {
      try (BufferedReader br =
                     new BufferedReader(new FileReader(path))) {
          return br.readLine();
      }
  }
  </code></pre>
  
  <p>As per doc ,</p>
  
  <blockquote>
    <p>The try-with-resources statement ensures that each resource is closed at the end of the statement. </p>
  </blockquote>
  
  <p>My question is,
  Why do I need to declare resource within parentheses immediately after the try keyword. <em>(like BuffereReader above)</em></p>
  
  <p>BuffereReader implements <code>java.lang.AutoCloseable</code> </p>
  
  <p>So why not support something like this,</p>
  
  <pre><code>static String readFirstLineFromFile(String path) throws IOException {
          try{
              BufferedReader br =
                         new BufferedReader(new FileReader(path))
              return br.readLine();
          }
      }
  </code></pre>
  
  <p>And just implicitly close resource object once out of try.
  (As it has implemeted AutoCloseable)</p>
  
  <p>I just had a thought, Why change in syntax.</p>
  
  <p>Please Read Question Properly, Its Regarding Syntax Only.</p>