title: Instantiate Java Scanner in class or method
body: <p>I have created a class to read user input. I am using scanner to get user input and sanitize it. When using scanner object, it struck me with a question which of these 2 approaches will be best practice:</p>
  
  <ul>
  <li><p>To create a Scanner reference in a class &amp; instantiate it -> use it in each method</p>
  
  <pre><code>private Scanner sc = new Scanner(System.in);
  
  public int readInt() {
  
     int input = sc.nextInt();           
     // some code to restrict input to integers only
     return input;    
  
  }
  
  public int readDouble() {
  
     int double = sc.nextDouble();
     // some code to restrict input to double only
     return input;    
  
  }
  </code></pre></li>
  <li><p>To create a Scanner reference a class -> instantiate it in each method -> use it in each method</p>
  
  <pre><code>private Scanner;
  
  public int readInt() {
  
     sc = new Scanner(System.in);
  
     int input = sc.nextInt();           
     // some code to restrict input to integers only
     return input;    
  
  }
  
  public int readDouble() {
  
     sc = new Scanner(System.in);
  
     int double = sc.nextDouble();
     // some code to restrict input to double only
     return input;    
  
  }
  </code></pre>
  
  <p>Imagine a scenario where an object of this class will be created in another class, and readInt() or readDouble() will be called several times. In that case which of the 2 above mentioned approaches will be more practical from security and memory standpoint? </p></li>
  </ul>
  
  <p>Or as one of the comments suggested none of the above are practical and the best way will be to pass a scanner object as a paramter to each of the methods ?</p>