title: Local variable defined in an enclosing scope must be final or effectively final
body: <p>I'm trying to print out the percentage of <code>x/mol</code>, but I cannot seem to get it to work.  I am getting a this error: <code>Local variable x defined in an enclosing scope must be final or effectively final</code></p>
  
  <p>It says this happens at line 22, and there is no easy fix for it.  What is java's scope, and how can I add x to the scope so that my timer can read it.</p>
  
  <pre><code>import java.math.BigInteger;
  import javax.swing.Timer;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.BufferedWriter;
  import java.io.IOException;
  
  public class mol {
      public static void main(String[] args) {
  
          String writable = "";
          BigInteger x = new BigInteger("0");
          BigInteger mol = new BigInteger("602214179000000000000000");
          File file = new File("molA.txt");
          BufferedWriter bw = null;
          FileWriter fw = null;
          Timer t;
          t = new Timer(10000, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println(x.divide(mol) + "%");
                  System.out.println(x);
              }
          });
  
          System.out.println("Starting...");
          t.start();
  
  
          do {
               writable += "a";
              x = x.add(new BigInteger("1"));
          } while (x.compareTo(mol) &lt; 0);
  
          try {
  
              fw = new FileWriter(file);
              bw = new BufferedWriter(fw);
              bw.write(writable);
  
              System.out.println("Done");
  
          } catch (IOException e) {
  
              System.out.println(e);
  
          } finally {
  
              try {
  
                  if (bw != null)
                      bw.close();
  
                  if (fw != null)
                      fw.close();
  
              } catch (IOException ex) {
  
                  System.out.println(ex);
  
              }
          }
          t.stop();
          System.out.println("Finished!");
  
      }
  }
  </code></pre>