title: When does a stream close if its not closed manually?
body: <p>I would like to know when does a stream close if its not closed manually. By this I mean, will the stream be closed if the scope of its reference is no more?</p>
  
  <p>Consider the following sample scenario.</p>
  
  <pre><code>Class A{
   InputStream in;
   OutputStream out;
   A(){
    // Initialize and create the streams.
   }
   ...
  }
  Class B{
   public void myMethod(){
   A a = new A();
   System.out.println("End of my method.")
   }
  ...
  }
  </code></pre>
  
  <p>Here, once I am done with the stream, I am exiting <code>myMethod()</code> but the program which in turn the process, is not terminated and goes on with other operations. </p>
  
  <p>I did not close the streams. Will it be closed automatically once the scope of the reference to class A ends? (ie. When <code>myMethod()</code> ends)? Does GC takes care of that? Also, I read that the streams will be closed once the process ends and the system releases all the resources held for it for other processes. How can we check whether the stream is open or not? Are there any linux commands or options from Java to check that? </p>
  
  <p>Any help is appreciated!</p>