title: Why doesn't try-with-resources work with field variables?
body: <p>This is my very first question on SO and I'm confused there isn't a similar question yet!</p>
  
  <p>So the question is:</p>
  
  <blockquote>
    <p>Why doesn't try-with-resources work with field variables?</p>
  </blockquote>
  
  <p>Or in other words: Why do I always need a local variable for that?</p>
  
  <p>Here goes some example code:</p>
  
  <pre><code>public class FileWriteTest {
  
      public FileWriter file;
  
      public void workingDemo() {
  
          try(FileWriter file = new FileWriter(new File("someFilePath")) {
              // do something
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  
      public void notWorkingDemo() {
  
          file = null;
  
          try(file = new FileWriter(new File("someFilePath")) {
              // do something
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>May anyone explain me why there is this convention?</p>