0

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
}
Tommy131313
  • 75
  • 1
  • 4
  • 4
    Scope is a compile time concept. It applies to the use of names in the program code. `aTest` is one such name, in this case a variable. At runtime, `aTest` contains a reference to an object. When `aTestCopy = aTest` runs, the value of the reference gets copied from `aTest` into `aTestCopy`. – Sotirios Delimanolis Jan 18 '22 at 17:30
  • 3
    The term “out of scope” makes no sense for objects. Only *variables* have scope, which tells whether the name is valid. Objects have *reachability*. If you [can] access an object through a variable (any will do), it’s reachable. – Holger Jan 18 '22 at 17:30
  • 2
    Regarding your other question, initializing a variable with `null` is not a good practice, but unavoidable in scenarios like this. Using a `boolean` variable wouldn’t help as the compiler would still complain if you don’t initialize the variable. – Holger Jan 18 '22 at 17:33
  • I'm wondering about this: `It's a shallow copy`. I read this as that you expect the assignment `aTestCopy = aTest;` to create a copy. Is that correct? – Erik Jan 19 '22 at 15:36

2 Answers2

1

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.

  • `Garbage collection is triggered whenever an object in memory is not pointed anymore by any reference.` This is not correct. There are lot's of possible reasons for a gc but this is not one. However, the object will collected during a gc if it's not reachable – Erik Jan 19 '22 at 15:34
  • I think we can't say **this is not correct** strictly. We can say, this is not the only reason. Because it is well-known mark and sweep algorithm that is used in garbage collection mechanisms. – Serhat Yılmaz Jan 19 '22 at 16:20
  • Err, I do think we can. There _might_ be some very specific edge cases where this could happen but in the vast vast majority of cases this is not true. It also doesn't matter what actual gc algorithm is used. – Erik Jan 20 '22 at 10:17
  • I'm btw well aware of how a mark-sweep gc works. – Erik Jan 20 '22 at 11:20
  • 1
    The statement “*Garbage collection is triggered whenever an object in memory is not pointed anymore by any reference*” is definitely wrong, regardless of the implementation, because it reverses cause and effect. Before the garbage collection, the JVM doesn’t know whether there are references to an object because that’s precisely what garbage collection will find out. So this information can’t be the trigger for the garbage collection. It’s the result. – Holger Jan 20 '22 at 14:39
0

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.

meriton
  • 68,356
  • 14
  • 108
  • 175