title: How do I set all the objects in a List to null?
body: <p>I am trying to do some clean up method. Where I have several fields and I want to call their respective cleanup methods and then set them to null.</p>
  
  <p>Just like this:</p>
  
  <pre><code>if(obj != null)
  {
      obj.cleanUp();
      obj = null;
  }
  </code></pre>
  
  <p>But instead of repeating the above several times I thought of using a method that would check them:</p>
  
  <pre><code>public void checkAndClean(ArrayList&lt;Object&gt; objs)
  {
      for(Object obj : objs)
      {
          if(obj != null)
          {
              obj.clean();
              obj = null;
          }
      }
  }
  </code></pre>
  
  <p>And I add all the objects to an ArrayList and then pass it to that method:</p>
  
  <pre><code>    ArrayList toClean = new ArrayList&lt;Object&gt;();
      toClean.add(obj1);
      toClean.add(obj2);
      checkAndClean(toClean);
  </code></pre>
  
  <p>However this doesn't work, my unit test shows that the objects are not null after calling this method. </p>
  
  <p>How can I set all the objects in a List to null?</p>
  
  <hr>
  
  <p>Test Code:</p>
  
  <pre><code>@Test
  public void test()
  {
      String string = "stuff";
      ArrayList toClean = new ArrayList&lt;Object&gt;();
      toClean.add(string);
      checkAndDestroy(toClean);
      assertEquals(null, string);
  }
  </code></pre>