When is variable 'a' likely to be garbage collected class A { static String a="Hello"; static String b="there"; public static void main(String args[]) { a="afsfa"; a=null; a=b; } } My doubt is whether the likely candidate for garbage collection is at line 1 or line2.no doubt setting a object reference to null makes it a candidate for garbage collection yet String is also immutable so when we assign a new value to a a new object is created which is passed a reference and this should i think be the first candidate for garbage collection? pls remove me out of this anamoly at to which is the first candiadate for garbage collection. e-mail at harveen@onebox.com
------------------
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
When a is set to afsfa the string "Hello" is available for garbage collection, however a is not because it has be re-set to a different address. When a is set to null it would be food for the gc(), except that the future reference protects it. Since this is a question about Java instead of about JavaRanch, I expect that this question is going to get moved, very shortly. You will probably find it in Java in General (Beginner).
"JavaRanch, where the deer and the Certified play" - David O'Meara