-1

If i have following code for a Java program

class Dotcom{
    private int positions[];// position is reference variable at class level
    //some other instance variables
    // suppose here we have setter for initialising "positions"
    public void Somefun (){
           int modifier [positions.length];
           // code for initialising "modifier"
           positions = modifier; 
    }
}

So my question is that when Somefun function returns then the portion of memory to which modifier is referencing will be eligible for garbage collection as modifier is destroyed.So does the statement

 positions = modifier;

makes sense as now to which area of memory positions is referencing?

OldSchool
  • 2,123
  • 4
  • 23
  • 45
  • Both the references will point to same object in memory. The reference is in `stack memory` not in `heap` and the actual object in in `heap memory`. – Braj Jun 09 '14 at 16:20
  • GC starts garbage collection from root, **not** from methods. As long as any single reference to memory location is accessible GC will **not** collect it. – Germann Arlington Jun 09 '14 at 16:22
  • 1
    The code you've given isn't valid. Did you mean `int[] modifier = new int[positions.length];`? It really helps if you post *actual* code rather than pseudo-code. (I'd also strongly advise you to follow the Java convention of keeping type information in one place, e.g. `private int[] positions` instead of `private int positions[]`.) – Jon Skeet Jun 09 '14 at 16:29
  • Why do you need to assign it in different local variable? Make it `final` so that it can be assigned at the time of object construction. – Braj Jun 09 '14 at 16:35

3 Answers3

1

Your current code will not compile (Java doesn't have static arrays). You instead need:

int[] modifier = new int[positions.length];

which creates a new block of memory on the heap for the int array and points modifier to it. Now when you do

positions = modifier;

positions is also pointed to that block of memory, and the block that positions used to point to can be garbage collected, assuming it isn't being referenced by another variable. It becomes easier to understand this if you think of all Java variables as pointers.


Initial
positions --------&gt (Allocated space 1)


int[] modifier = new int[positions.length];
positions --------&gt (Allocated space 1)
modifier  --------&gt (Allocated space 2)


positions = modifier;
                    (Allocated space 1)
positions --------&gt (Allocated space 2) &lt-------- modifier

Notice that now nothing is referencing Allocated space 1, so it can be freed by the garbage collector.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

This is a very common way for people to set and modify instance variables. The garbage collector will not pick up positions, but it will pick up the OLD address to which positions used to point.

Your code will still work as expected using this method.

Adam Yost
  • 3,616
  • 23
  • 36
0

This might help you to understand the references.

Here is the sample code:

public static void main(String[] args){
    Dog d = new Dog();
    go(d);
}

public static void go(Dog dog){...}

Here d and dog both the references will point to the same Dog object in the The Heap memory and the references will reside in The Stack memory that will be destroyed once the scope of that method is finished.

enter image description here


Is it safe to assign reference variable,which is of method level to Class level reference variable?

If you have the reference of any object then you are free to do anything with it. Try to avoid such type of assignment that might result in a bug in later stage of the application and it's very difficult to detect such type of bugs because you don't know which reference has changed the value.

Braj
  • 46,415
  • 5
  • 60
  • 76