title: When Is The Object Eligible For Garbage Collection?
body: <p>In the code below, given that <code>amethod</code> has been called. At what point/line is the Object originally referenced by <code>myObject</code>, eligible for Garbage Collection?</p>
  
  <pre><code>class Test {
    private Object classObject;
  
    public void amethod() {
      Object myObject = new Object();
      classObject = myObject;
      myObject = null;
    }
  }
  </code></pre>
  
  <p>And if <code>classObject</code> or <code>amethod</code> had an access modifier of public, protected, default or static, would it affect what point the Object is eligible for Garbage Collection? If so, how would it be affected?</p>
  
  <ul>
  <li>My first thought is that the Object is eligible for Garbage Collection when the Test object is eligible for Garbage Collection.</li>
  <li>But then again. The optimizer may know that the classObject is never read from in which case <code>classObject = myObject;</code> would be optimized out and <code>myObject = null;</code> is the point it is eligible for Garbage Collection.</li>
  </ul>