Hi,
I came across following question.
At what point will the object created on line 8 be eligible for garbage collection?
1.public class RJMould{
2. StringBuffer sb;
3. public static void main(
String argv[]){
4. RJMould rjm = new RJMould();
5. rjm.kansas();
6. }
7. public void kansas(){
8. sb = new StringBuffer("Manchester");
9. StringBuffer sb2 = sb;
10. StringBuffer sb3 = new StringBuffer("Chester");
11. sb=sb3;
12. sb3=null;
13. sb2=null;
14.
15. }
16.}
The given answer is Line 13.
My doubt:
Object sb is assigned value at line 8.
sb is again assigned some value at line 11.
Line 12 makes sb3 null and line 13 makes sb2 null . But still sb holds some value.
So how could sb is eligible for garbage collection at line 13? Isn't it hold some reference(which point to object creted with sb3 ref)?
According to me it should be eligible for GC after execution pointer is out of main() method ie. after programe is ended.
Please expalin me if I am missing some concepts.
Thanks in advance,
Vivian