-1

I made 3 objects with same reference variable and stored all of them in array, When I print those reference variable all of them show different addresses.I was told if some object loose its reference variable GC removes it from heap, so how come same reference variable showing up 3 difference location, GC did not work? there are actually 3 objects in heap or same reference variable has 3 memory address

class MyClass {

    public static void main(String[] args) {
        MyClass[] i = new MyClass[3];

        for (int j = 0; j <= 2; j++) {
            MyClass p = new MyClass();
            i[j] = p;
        }

        for (int k = 0; k <= 2; k++) {
            System.out.println(i[k]);
        }
    }
}

Output :

[shadow@localhost String]$ javac MyClass.java 
[shadow@localhost String]$ java MyClass 
MyClass@138532dc
MyClass@dce1387
MyClass@54640b25
[shadow@localhost String]$
Einar
  • 1,001
  • 3
  • 11
  • 28
PankajKushwaha
  • 878
  • 2
  • 11
  • 25

3 Answers3

1

You still have a reference to the objects inside the array and you have a reference to the array within your program. The GC cannot act on those objects.

I was told if some object loose its reference variable GC removes it from heap

Garbage collection will occur if no reachable references to the object remain.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • But array hold the reference variable , not the address , and each time I change the address.So how same reference variable can point to 3 different location. – PankajKushwaha May 26 '14 at 23:23
  • @user3025905 You'll want to read [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) very carefully. A variable (of a reference type) holds a value which is the reference to an object. When you assign it to another variable (or array element), you are copying the value of the reference and assigning the copy. – Sotirios Delimanolis May 26 '14 at 23:25
  • Now I understand, Thank You , I can't vote up as I just joined StackOverflow but yes Your answer helped! – PankajKushwaha May 26 '14 at 23:30
0

You wrote

I was told if some object loose its reference variable GC removes it from heap

The GC can only remove an object from the heap if it loses all its references that are reachable from the stack, not just one of them. By keeping references to all three objects in the array called i, you are saving them from being garbage collected.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Each time I am holding same reference variable in the array, so if refrence variable p was pointing to an object and I made new object with same reference variable so the address in that reference variable changed , and it will point to newly created object and loose the address of previous one ( So it should be removed by GC ) , no matter how many times I hold the reference variable in different locations I change the address inside them by creating new object each time. – PankajKushwaha May 26 '14 at 23:28
  • 1
    You're **not** putting the same value in the array three times. You're putting three different values in the array. – Dawood ibn Kareem May 26 '14 at 23:30
0

To answer the question in your title, because you assigned three successive different values to it inside the loop.

user207421
  • 305,947
  • 44
  • 307
  • 483