I was just wondering if we should do obj = null
after adding it into an array. Note that this is about a special case, read below.
First, let's consider class A
like this:
public class A {
public void doSomething(B object) {
// some code here
}
}
and class B
like this:
public class B {
private final A a;
public B(A a) {
this.a = a;
}
public void aMethod() {
// Does something here
a.doSomething(this);
}
}
Until now everything's ok. An object of type B
can call the A
's doSomething()
method to edit the object based on some data there.
Now let's consider the following situation: the doSomething()
adds the object B to an arraylist. Now which if these codes should I use:
public void doSomething(B b) {
arrayList.add(b);
b = null;
}
// or
public void doSomething(B b) {
arrayList.add(b);
}
In the first doSomething()
we set the object to null, which means that the method caller becomes null. Well, that's OK to me as doSomething()
call is the last statement I need to in my aMethod()
. But is it good to do so?