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.
Just like this:
if(obj != null)
{
obj.cleanUp();
obj = null;
}
But instead of repeating the above several times I thought of using a method that would check them:
public void checkAndClean(ArrayList<Object> objs)
{
for(Object obj : objs)
{
if(obj != null)
{
obj.clean();
obj = null;
}
}
}
And I add all the objects to an ArrayList and then pass it to that method:
ArrayList toClean = new ArrayList<Object>();
toClean.add(obj1);
toClean.add(obj2);
checkAndClean(toClean);
However this doesn't work, my unit test shows that the objects are not null after calling this method.
How can I set all the objects in a List to null?
Test Code:
@Test
public void test()
{
String string = "stuff";
ArrayList toClean = new ArrayList<Object>();
toClean.add(string);
checkAndDestroy(toClean);
assertEquals(null, string);
}