title: Why do we have to do both declare and define resources in try-with-resource block?
body: <pre><code>try(PrintWriter f = new PrintWriter(new BufferedWriter(new FileWriter("abc.txt")));) 
  {}
  catch(IOException ex) 
  {
    ex.printStackTrace();
  }
  </code></pre>
  
  <p>Above works fine. But when I do </p>
  
  <pre><code>PrintWriter f;
  try(f = new PrintWriter(new BufferedWriter(new FileWriter("abc.txt")));) 
  {}
  catch(IOException ex) 
  {
    ex.printStackTrace();
  }
  </code></pre>
  
  <p>It throws errors. Why is it so? I was testing this new feature and I was of the opinion I would take the 2nd method and after the <code>try-catch statement</code> would print the resource <code>PrintWriter f</code> - which should be null if try-with-resource statement works as expected. Why is the 2nd way not allowed?</p>
  
  <p>Also how could I test it by method 1?</p>