-2

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);
}
Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • 2
    Assuming you gave someone the address of your office in a piece of paper, if the person tears up the paper some time later will your office building come crushing down? – Igwe Kalu Jan 14 '16 at 01:37
  • @IgweKalu Don't try it – Scary Wombat Jan 14 '16 at 01:37
  • 2
    Why? The normal thing would be to just clear the list. A list full of null references isn't any use to anybody. – user207421 Jan 14 '16 at 01:38
  • @ScaryWombat, not 'me' anymore. I hope 'someone' doesn't try it too. – Igwe Kalu Jan 14 '16 at 01:39
  • 1
    It's actually: I got 2 addresses from my 2 friends (obj1, obj2), I gave you a piece of paper with a list of the addresses written on it. You then read out the list items one by one and give them to your friend on the phone (obj). He records the phone call, then deletes it. Now you expect my 2 friends (obj1, obj2) to be deleted :) – Gavriel Jan 14 '16 at 01:41
  • 1
    Note also that cleanupmethods like this are rarely actually necessary in Java. – Louis Wasserman Jan 14 '16 at 01:48
  • @Aequita If you want a particular variable to reference null, then you must assign null to the variable in question exactly. – Igwe Kalu Jan 14 '16 at 01:55
  • @IgweKalu okay I think I understand a bit better, thank you for help – Aequitas Jan 14 '16 at 01:59

3 Answers3

1

obj is a reference, and setting it to null doesn't change anything in the ArrayList.

Even if you would change the list items, obj1 and obj2 are references to 2 objects. Inside the ArrayList you set the references of the items to null, but that doesn't change the obj1,obj2 reference

Gavriel
  • 18,880
  • 12
  • 68
  • 105
1

If you want to get the list of null references, you may use List.set method:

public void checkAndClean(ArrayList<Object> objs)
{
    for(int i=0; i<objs.size(); i++)
    {
        Object obj = objs.get(i);
        if(obj != null)
        {
            obj.clean();
            objs.set(i, null);
        }
    }
}

However probably you just need to remove all the elements from the list. In this case you may call objs.clear():

public void checkAndClean(ArrayList<Object> objs)
{
    for(Object obj : objs)
    {
        if(obj != null)
        {
            obj.clean();
        }
    }
    objs.clear();
}
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
0

If I understand you question correctly, you have just bunch of objects (not already in the list) and want to cleanup each of them.

Then you can do it like this without temporary list.

obj1 = checkAndClean(obj1);
obj2 = checkAndClean(obj2);

public Object checkAndClean(Object obj) {
    if(obj != null) {
        obj.clean();
    }
    return null;
}

The method returns null for brevity of code which uses it. It can be void of course but then you need call it and then still do obj1 = null;.

Update with new info

You can use generics if all your objects have different classes.

I'll show you on example of Number abstract class which has many implementations like Integer or Dobule.

Integer obj1 = 1;
Double obj2 = 2.0;

obj1 = checkAndClean(obj1);
obj2 = checkAndClean(obj2);
System.out.println("" + obj1 + ", " + obj2);

public <T extends Number> T checkAndClean(T obj) {
    if(obj != null) {
        obj.intValue(); // your cleanup method
    }
    return null;
}
Ruslan Stelmachenko
  • 4,987
  • 2
  • 36
  • 51
  • why not just return `null` – Scary Wombat Jan 14 '16 at 01:40
  • this is not so different from what OP did already. – Igwe Kalu Jan 14 '16 at 01:40
  • Beacause the OP hasn't any list. Read the question more carefully. It puts his objects to the list just to call clean method. So yes this isn't much dirrerent from the OPs code but it gets rid of unnecessary temporary list and "fixes" the problem with nullifying references. – Ruslan Stelmachenko Jan 14 '16 at 01:43
  • anyway `obj = null;` as in your answer won't make any element of the list null. – Igwe Kalu Jan 14 '16 at 01:49
  • What list? There are no any lists here. Only objects. But yes `obj = null` is redundant in this case because we returns null in any case. I will correct it. – Ruslan Stelmachenko Jan 14 '16 at 01:51
  • @djxak it seems what I wanted isn't possible so I'm going to go with your alternative which still accomplishes my goal of avoiding multiple if(obj != null). When trying to implement it this way eclipse is wanting me to do a cast on the object being passed in as well as on the method like so: `obj1= (obj1Class) checkAndClean((InterfaceAllTheObjsHave) obj1);` the class of all the objects are different, but the interface is the same, is there a way to avoid all the casts? note the checkAndClean method currently takes an argument of type InterfaceAllTheObjsHave – Aequitas Jan 14 '16 at 02:12
  • In my code the `Object` is just for example. It can be any class/interface of course and if `checkAndClean` method receives parameter of type `InterfaceAllTheObjsHave` then it definitely should return `InterfaceAllTheObjsHave` and references to this objects in your code also should have be interfaces. If they must be concrete implementations then you can parametrize your `checkAndClean` method with generics. I'll update my answer to show how. – Ruslan Stelmachenko Jan 14 '16 at 02:24
  • Thanks! works great! – Aequitas Jan 14 '16 at 02:37