title: Java: How many Objects created? Array as member variable
body: <p>I got a question about the following piece of code.</p>
  
  <pre><code>class MemArr{
    int[] mem = {1,2};
  }
  
  public class Test{
    public static void main(String[] args){
      MemArr[] x = new MemArr[2];  //line 1  
      x[0] = new MemArr();         //line 2
      x[1] = null;                 //line 3  
                                   //line 4
    }
  }
  </code></pre>
  
  <p>How many objects are created in total and how many objects are eligible for garbage collection when line 1 is reached?</p>
  
  <p>I think, that at line 1, there were 5 objects created (1 array x, 2 objects of <code>MemArr</code> in the array x, 2 member variables of integer array for the objects of MemArr)</p>
  
  <p>At line 2, two objects were created (one <code>MemArr</code> object and its member variable <code>mem</code>).</p>
  
  <p>At line 3 when <code>x[1]</code> is set to null, I think that there were in total 4 objects which are eligible for the GC, but I am not very sure about this.</p>