8

In Java, how would I do the following:

Foo bar = new Foo();
Foo a = bar;
Foo b = bar;
bar = null;//will only set bar to null. want to set value of bar to null rather than the reference to null.

Is it possible to set the variables bar, a and b (all are a reference to bar) to null only with access to bar? If so, could somebody please explain how to do so.

rodit
  • 1,746
  • 2
  • 19
  • 33
  • 2
    No, this is not possible in Java. The rules about assigning variables mean that you must use the name of a variable to assign to it. – Nayuki Nov 08 '15 at 16:13
  • I pretty much think that when you set bar to null, then a and b have null reference as well. Or what is the problem? – The Law Nov 08 '15 at 16:13
  • Please read this tutorial: http://www.javaranch.com/campfire/StoryPassBy.jsp – Nayuki Nov 08 '15 at 16:14
  • @TheLaw Re: I pretty much think that when you set bar to null, then a and b have null reference as well. In this case a and b are separate objects with copies of the data in bar from new Foo(). – Jason K. Nov 08 '15 at 16:17

3 Answers3

6

No, it is not possible in Java.

I a little explain what happened in your code. Here Foo bar = new Foo(); you created object of Foo and put reference to the variable bar.

Here Foo a = bar; and Foo b = bar; you put the reference to the variables a and b. So you have now one object and three variables pointing to him.

Here bar = null; you clear the bar variable. So you have two variables (a and b) pointing to the object and one variable (bar) without reference.

Mateusz Korwel
  • 1,118
  • 1
  • 8
  • 14
5

That's not possible in Java.You can not set reference a and b null using bar.

Reason - Java is pass by value not pass by reference.

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • 14
    I don't think this is the right reason. "pass by value/reference" are evaluation strategies for method calls. No method is called here. – xehpuk Nov 08 '15 at 23:47
2

There isn't really a destructor in Java like there is in C:

If you knew that you wanted to set multiple objects to null, then you would probably use an array of Foo rather than declaring them as separate objects. Then use loops for consturctor calls, instatiation /initialization.

Foo bar = new Foo();
Foo array[2]
for (int i=0; i<array.length; i++) {
    array[i] = bar;
}

Then you could use a loop at the end

for (int i=0; i<array.length; i++) {
    array[i] = null;
}

This is the strategy you would want for Data Structures because you can handle any number of objects, for recursion etc.

Community
  • 1
  • 1
Jason K.
  • 407
  • 4
  • 12
  • BTW, when you say a = bar, you're copying the value of bar to a. changing bar will not change the copied values in other memory locations. I know math people sometimes assume that that equal sign continues to be true after the fact, in C and Java that's a double == is bi-symmetric. The single = is called an assignment you can think of it more like an asymmetric arrow to the left. In Java when you set an object to null that's the C equivalent of calling a destructor. – Jason K. Nov 09 '15 at 16:14
  • Re: "want to set value of bar to null rather than the reference to null" The ASCII value 0 literally represents null, which by definition is the end of a string or object (that's why we use to call them null-terminated strings). So if the value in memory was changed to null then it wouldn't be a Foo object anymore, as it wouldn't have the necessary member functions, member variables etc as defined in the class. – Jason K. Nov 09 '15 at 16:22