This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In gc , if an array is set to null, eligible for gc . does the elements of array which are referencing to some objects are also eligible,
An array object is eligible for gc when it is no longer reachable. Setting an array reference that refers to the array, to null, doesn't mean that the array is eligible for gc. For example...
The array object that is referred to by "o" isn't eligible for gc, when the "o" variable is set to null. This is because it is still reachable by the "cpy" variable. The array object will be eligible when the cpy variable gets set to null, or goes out of scope.
This also applies to the elements in the array. For example...
In the case, the array object is not longer reachable, and hence eligible for gc. As for the elements: The index zero element is no longer reachable, and hence, eligible for gc. The index one element is still reachable via the "c" variable, and hence, not eligible for gc.
Which brings us to the last point...
when the object referced o will be eligible for gc..
You have to remember that objects are eligible for gc -- not references. They merely get accessed, changed (if not final), or go out of scope.
when a local object is eligible for gc ,after return statement or after end of method '}' if it is not goint to return.
An object is eligible for GC when it is no longer reachable, period. If a variable goes out of scope, it doesn't mean that the object is no longer reachable -- there may be another reference, or the object reference may be the returned value.
object refernced o , is not eligible till end of method as oa is refering to it. so it will be eligible for gc after line 1 or after line 2.
In your example, does it make a difference? But if we modify the example a bit...
The answer is when the "oa" reference variable goes out of scope. This could be after line 1 or line 2 is executed, depending on exactly which return statement is actually executed.