Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
class beta{
}
class demo{
	static beta b1;
	beta b2;
	public static void main(String args[]){
		beta b1 = new beta();
		beta b2 = new beta();
		demo d1 = new demo();
		demo d2 = new demo();
		d1.b1=b1;
		d1.b2=b1;
		d2.b2=b2;
		d1= null;
		b1= null;
		b2=null;
	
	}
}


If we add System.out.println(d1.b1); inside main block then the output is beta@3a7f1228
i.e; not null.

What I have tried:

As we can still access d1.b1 so object of d1 is still alive, thus none should be available for gc.
Tell me where i am wrong?
The answer is 1.
Posted
Updated 2-Nov-17 8:59am
v2

You have declared b1 as static so it always exists.
 
Share this answer
 
Comments
Member 13409417 2-Nov-17 13:51pm    
Yeah but is d1 object eligible?
Richard MacCutchan 2-Nov-17 14:01pm    
Yes, but you have no code following those three statements so there is nothing for the garbage collector to do.
Please stop asking this type of question.

As already stated all the objects are available for garbage collection after the main() method ends since the program terminates and there is no actively referenced objects in memory.
 
Share this answer
 
Comments
Member 13409417 2-Nov-17 13:49pm    
@Mehdi Gholam You are not getting my question, i am asking for the eligibility just after the line b2=null;
Mehdi Gholam 2-Nov-17 13:55pm    
Like I said all of them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900