title: Java Scanner Best Practice
body: <p><em>Question:</em>
  I have a method that Instantiates a new Scanner Class with a BufferedReader, and returns the Scanner Class itself. As far as I understand it, Java will return the Scanner class - as a "copy" of the object (pass-by-value), as opposed to what we would call in C++ a Reference or Pointer. Provided that logic is correct - does that then mean, the Scanner instantiated in the method I call, will continue to stay OPEN and thus never be garbage collected?</p>
  
  <p>For example: (bare bone)</p>
  
  <pre><code>private Scanner getDataFromWebService(String url)
  {
      URLConnection sc = null;
      URL test = null;
      Scanner scanner = null;
  
      test = new URL(url);
      sc = test.openConnection();
      in = new BufferedReader(new InputStreamReader(sc.getInputStream());
  
      scanner = new Scanner(in);
  
      return scanner;
  }
  
  // Sample Call
  
  Scanner newScanner = getDataFromWebService(url);
  </code></pre>
  
  <p>Secondly if you would be so kind to oblige; what happens then to the URLConnection and the BufferedReader, because if I close them before returning the Scanner in the getDataFromWebService Method - it will 'nullify' the returned Scanner. Perhaps the answer to this lies in the first question.</p>
  
  <p>What would be best practice here, just never returning a complex object......</p>