2

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Chaoz
  • 2,119
  • 1
  • 20
  • 39
  • 5
    No, the method caller does not become null. Java is pass by value. – markspace Jun 03 '15 at 16:44
  • 1
    you need to read about [The Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter) as well. –  Jun 03 '15 at 16:47
  • 2
    I'm confused, what are you trying to accomplish by setting `b = null;`? There's no case where it makes sense to do it, but if you explain why you think it's a good idea or what you think it does then I or another can help you learn why it's basically doing nothing in the grand scheme of things. – Captain Man Jun 03 '15 at 16:48
  • they think they are affecting the reference that was passed in, and it Java it does not work that way, which makes the entirety of the question a moot point –  Jun 03 '15 at 16:50
  • But it's also tagged memory-leaks which makes me wonder if OP thinks this frees memory or something. – Captain Man Jun 03 '15 at 16:50
  • @JarrodRoberson: I don't disagree, but that doesn't mean that they're asking if Java is pass-by-value or pass-by-reference. There's a fundamental misunderstanding in the way before one could even get to that discussion. – Makoto Jun 03 '15 at 16:51
  • 1
    root cause is root cause same answer –  Jun 03 '15 at 16:51
  • @CaptainMan You are right, I really wasn't sure about this code. – Chaoz Jun 03 '15 at 16:56

2 Answers2

8

Just use:

public void doSomething(B b) {
    arrayList.add(b);
}

There is absolutely no need to set the parameter to null as it (the parameter, not the object that it refers to) no longer exists when the method exits.

Re

Note that this is about a special case, read below.

I see nothing special about your case.

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?

Objects cannot be set to null, only variables and parameters (which are variables) can. You're confusing references with reference variables, a core distinction in Java.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Objects cannot be set to `null`. This single statement summarizes everything there is to `pass-by-value` semantics in Java. +1 – Chetan Kinger Jun 03 '15 at 16:51
  • Thanks to both you and @makato. I wasn't really sure about this, because it's not exacly "pass-by-value". It is a reference until you set it's value, because if you do `object.something()` it actually modifies the original `object`. – Chaoz Jun 03 '15 at 16:52
  • Not exactly. You're modifying an attribute of the original object, but that's not the same as *the* original object. – Makoto Jun 03 '15 at 16:54
  • It is the same when editing a field or calling a function but not the same when changing it to something else with `=`. (This is what I believe because) How would Metrics work otherwise? You only have a final declaration of it and call methods to update it by passing the metrics and the methods modify the fields. – Chaoz Jun 03 '15 at 16:59
5

No; it conflates the responsibility of doSomething too much. Why should it care what the lifecycle of whatever it has inserted in is?

It's also not going to have any effect. Due to Java being pass-by-value, you're only modifying the scoped value of b, and not wherever b originated from.

Code like that will only serve to confuse someone reading it down the road (and perhaps yourself), as the behavior in which you portray is misleading. If you're concerned about the lifecycle of the object you're inserting, note that so long as there's a reference it will not be eligible for garbage collection. This means so long as the collection you're adding it to isn't eligible, that reference isn't eligible either.

Makoto
  • 104,088
  • 27
  • 192
  • 230