-1

I have this code which adds some objects to an ArrayList. Then, from the List, in Step 1, I modify the contents of these objects -- and the backing objects themselves also change, which works. Then, in Step 2, from the List I set my objects to NULL. The list changes - I can see its elements are NULL, but the backing objects do not change. Why is that?

My expectation: a, b, c are set to NULL.

public class Test {

static class Student
{
    String name;
    int age;

    public Student(String name, int age) { this.name = name; this.age = age; };
    public String getName() { return name; }
    public int getAge() { return age; }
    public void setName(String name) { this.name = name; }
    public void setAge(int age) { this.age = age; }
}

public static void main(String[] args)
{
    Student a = new Student("John", 20);
    Student b = new Student("Mary", 21);
    Student c = new Student("Bill", 22);

    ArrayList<Student> students = new ArrayList<Student>();
    students.add(a);
    students.add(b);
    students.add(c);

    // STEP 1: Changing contents from list: THIS WORKS - Referenced objs change (a, b, c)
    for (int i = 0; i < students.size(); i++)           
        students.get(i).setName("NEW NAME");


    // STEP 2: Setting to null from list: DOES NOT WORK - Referenced objs do NOT change (a, b, c)
    for (int i = 0; i < students.size(); i++)
        students.set(i, null);


}
gene b.
  • 10,512
  • 21
  • 115
  • 227
  • Yeah, that's how references work. What were you expecting to happen? – dnault Feb 23 '16 at 20:14
  • Did you read "Expectation" at the top? I need a,b,c to be NULL. – gene b. Feb 23 '16 at 20:15
  • I am blind, sorry :-) – dnault Feb 23 '16 at 20:16
  • [This answer](http://stackoverflow.com/a/12429953/1079354) and [this answer](http://stackoverflow.com/a/7034719/1079354) explain what's happening with your references. – Makoto Feb 23 '16 at 20:21
  • Simply assign the arraylist to null. Why take so much pain. – Pritam Banerjee Feb 23 '16 at 20:22
  • @PritamBanerjee: That would be conterproductive, most likely. In the grand scheme of things, since this is the last action in `main`, there's really no reason to set these things to `null` unless there's more code that you're not sharing. – Makoto Feb 23 '16 at 20:22

1 Answers1

0

array list simply holds the references to your objects. so when you set the reference to null it does not delete the backed object. try reading this article about how data is referred to in java.

Shawn
  • 403
  • 8
  • 38
  • but then how would I set my backed objects to NULL? That's what I need to do. – gene b. Feb 23 '16 at 20:21
  • @geneb. You have no choice but to do it one by one: `a = null; b = null; c = null;`. – Louis Wasserman Feb 23 '16 at 20:24
  • all objects are only deleted when the garbage collector finds no more references to them. so if you set everything to null in your arraylist and then set a, b and c to null then your objects will be deleted. you can also try not to save a, b and c and just directly add them to arraylist. like students.add(new Student("John", 20));, now if you students.set(0, null); it will be completely removed – Shawn Feb 23 '16 at 20:24