0

When I tried self-answering this question, I could only find other questions regarding removing items from an array so that items past the removed object get moved down 1 in the index.

In the code below, I have two arrays that hold the class SomeObject (with a name property): SomeObject_ONE and SomeObject_TWO.

Below I put an object "some-object" in the first array, and then set the second array to equal the object in the first array. I can still change the properties of the object in the first array by changing the properties of the object in the second array (the 3rd indent.)

However, when I try setting the object in the second array to null, it doesn't set the first array to null, rather it only removes the index for the object in the second array (4th indent).

Is there anyway to delete an object in an array rather than removing its array index?

Thanks.

    SomeObject[] ObjectArray_ONE = new SomeObject[10];
    SomeObject[] ObjectArray_TWO = new SomeObject[10];

    ObjectArray_ONE[0] = new SomeObject();
    ObjectArray_TWO[0] = ObjectArray_ONE[0];

    System.out.println(ObjectArray_ONE[0].name+" outputs default name");
    ObjectArray_TWO[0].name = "Name changed from OA2";
    System.out.println(ObjectArray_ONE[0].name+" outputs Name changed from OA2");

    System.out.println("Should not be null        : "+ObjectArray_ONE[0]);
    ObjectArray_TWO[0] = null;
    System.out.println("Should be null            : "+ObjectArray_ONE[0]);
    ObjectArray_ONE[0] = null;
    System.out.println("Only now is it really null: "+ObjectArray_ONE[0]);
Snowybluesky
  • 47
  • 2
  • 7
  • 1
    It's entirely unclear what you mean by "removes the index for the object". Note that the array doesn't contain objects - it contains references. Currently, you have two arrays where the array element values both refer to the same object. – Jon Skeet Jul 05 '17 at 21:54
  • Thanks. I guess I didn't really understand how arrays reference objects, I should probably delete this question for being bad xd. – Snowybluesky Jul 05 '17 at 21:58
  • The question isn't bad/wrong by itself, it can be useful for other people, so I suggest not deleting it. It asks about the basic concepts of array and object/pointer treatment in Java. As @JonSkeet suggests, you're wrong with the assumption that setting an item to null deletes it from ObjectArray_TWO. It does not, try printing ObjectArray_TWO[0] at the end of your program. If you rethink the paragraph about the 4th indent, the question would become good ;) – Martin Pecka Jul 06 '17 at 00:12

2 Answers2

1

This is a Pass by Reference vs Pass by Value.

Java is pass by value, and in this case the value is a reference. It's a subtle distinction. What it means here is that if you change the object being referenced in one place, it will change everywhere. But it will not change the reference. If I set the reference to be equal to something else, the original object is unchanged.

MaxPower
  • 881
  • 1
  • 8
  • 25
1

The reason for this is exactly what Max said. Its the same reason that if you set one of your method parameters to null (inside the method) it won't affect the caller.

If you want you can store an array of wrapper classes of that object and then calling the setter of the wrapper class... Not exactly neat though.

PS. Don't capitalise variable names... it confuses people.

Arthur Ceccotti
  • 380
  • 2
  • 6