Java referencing an out-a-scope object

281 views Asked by At

I'm saving the previous value of a Java object. I (almost) understand references are strong, weak, etc. but I can't find an example that classifies my specific situation that is apparently referencing an out-of-scope object. (I've looked at a lot of posts on this subject.)

Is the Java reference aTestCopy to the out-of-scope array of objects aTest valid?

It's a shallow copy but is it subject to garbage collection thus I need a deep copy for it to work reliably every time? Or should the declaration of the new object aTest be placed outside the while with the "previous" copied object aTestCopy?

Is the use of a null a good practice for the first-time-through switch or should that be a separate boolean variable? Thanks.

Tester[] aTestCopy = null;
while(true)
{
    Tester[] aTest = myMethodReturnsATesterArray(); // new values

    // need code to skip using aTestCopy the first time through - no previous value
    // else use the previous value here

    aTestCopy = aTest; // save them for the next use of previous values
}
2

There are 2 answers

5
Serhat Yılmaz On

There is no problem to use null as initial value for reference types in Java. But there isn't any relevance between initialize a reference type as null and garbage collection. You should initialize aTestCopy as null to use in another scope like in your case in while.

Garbage collection is triggered whenever an object in memory is not pointed anymore by any reference.

When you assign a reference type to another reference type (if it is not immutable in this case), you made copy the memory address of that value. So when you can make a change in aTestCopy will be reflected to the reference aTest.

0
meriton On

In Java, variables do not hold objects, only references to objects.

The lifetime of objects is largely independent of the lifetime of variables: Objects are created by executing new, and reclaimed once the program has relinquished all means of accessing them (i.e. the object is no longer reachable form the references the program holds).

Put differently, the first variable to hold a reference to an object holds no special significance. In particular, it does not determine the lifetime of that object. Any reference will keep the object alive, and there can be no such thing as an "out of scope" object.

Similarly, when you assign a variable of reference type, you are only assigning the reference, causing both variables to point to the same object, and to see any changes done to this object though the other variable.

If you want a new object, you must use new (or call a method that uses new).

In your case, I'd make sure that myMethodReturnsATesterArray returns a new array, referencing new Tester objects. Then, the objects created by separate iterations of the loop will exist independently, allowing the loop to reference both the old and new objects and do whichever comparision it deems appropriate, without having to fear that the invocation of myMethodReturnsATesterArray has somehow changed the previous object.