title: Interview question: Objects eligible for garbage collection
body: <p>Give the following code:</p>
  
  <pre><code>class A {
      Boolean b;
      A easyMethod(A a){
          a = null;
          return a;
      }
      public static void main(String [] args){
          A a1 = new A();
          A a2 = new A();
          A a3 = new A();
          a3 = a1.easyMethod(a2);
          a1 = null;
          // Some other code 
      }
  }
  </code></pre>
  
  <p>The question is how many objects are eligible for garbage collection right before <code>// Some other code</code>.</p>
  
  <p>Then correct answer is (at least that's the interviewer answer): <strong>2</strong> - the Boolean <code>b</code> because it's a wrapper and <code>a1</code> .</p>
  
  <p>Can you please me explain me why <code>a2</code> and <code>a3</code> aren't being garbage collected ? </p>
  
  <p><strong>LATER EDIT:</strong></p>
  
  <ul>
  <li>Ok, I think I get it now. It was a bit confusing at first, but now i am sure the interviewer was wrong. My initial mistake was that at first I didn't consider that Java is pass by value only, so it's impossible to make a2 null from inside a function that take "a2" as a parameter, because that a2 is actually a copy of a2. </li>
  <li>The part with the Boolean b was indeed quite obvious.</li>
  </ul>
  
  <p>Thanks for an answer, I will send some interview feedback after that :).</p>