title: Call Scanner within Try{} from another method? --Java
body: <p>My problem lies with trying to access <code>Scanner scan</code> created in one method from another method. It says it cannot find variable scan. I tried declaring a global <code>Scanner scan</code>, but it gave me an error, <code>non static variable name cannot be referenced from a static context</code>. How can I access this variable? </p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class MyClass {
      public static void myMethod() {
          final File f = new File("file.txt");
          Scanner scan = null;
          try {
              scan = new Scanner(f);
          }
          catch(FileNotFoundException ex) {
              System.exit(0);
          }
      }
  
      public static boolean anotherMethod() {
          final String s = scan.next ();
  
          if (s.equalsIgnoreCase ("true"))  return true;
          if (s.equalsIgnoreCase ("false")) return false;
  
          throw new java.util.InputMismatchException ();
      }
  }
  </code></pre>