This question is from whizlabs
how many objects are eligible for garbage collection after the execution of line 8?
class gc{
public static void main(
String args[]){
Object obj=new Object();
Object o1=foo(obj);
Object o2=new Object(); .. line 7
o2=o1; // line 8
//more code
}
public static Object foo(Object o){
o=new Object();
return o;
}
}
the answer is 1 explaining that o2=o1 makes o2 refer to the object referred by o1 hence the prev object created at line 7, which was being referred by o2 earlier becomes eligible for garbage collection
can anyone plz explain this....
thanks