There is only one variable named obj in the given code and that variable is declared at line //1. So, I am not sure what is the confusion about the variable. 
Please remember that variable and the object to which it points are two different things.
			
			
									
									
						About Question enthuware.ocajp.i.v7.2.920 :
Moderator: admin
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
- 
				morarumaria1988
 - Posts: 5
 - Joined: Mon Oct 30, 2017 5:17 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.920 :
It's clear now, thanks!
			
			
									
									
						- 
				demetrio
 - Posts: 16
 - Joined: Mon Sep 30, 2019 11:40 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.920 :
Suppose the question code was (I just added one println line):
public class NewClass {
private Object o;
void doSomething(Object s){
o = s;
//line bellow was added
System.out.println("the second time this line is printed is the exact moment "
+ "when the object created in line 1 can be Garbage Collected"); //line 7
}
public static void main(String args[]){
Object obj = new Object(); // 1
NewClass tc = new NewClass(); //2
tc.doSomething(obj); //3
obj = new Object(); //4
obj = null; //5
tc.doSomething(obj); //6
}
}
Am I right? I mean, the second time line 7 is evoked is the exact moment where the object created in line 1 is eligible for garbage collector, right?
			
			
									
									
						public class NewClass {
private Object o;
void doSomething(Object s){
o = s;
//line bellow was added
System.out.println("the second time this line is printed is the exact moment "
+ "when the object created in line 1 can be Garbage Collected"); //line 7
}
public static void main(String args[]){
Object obj = new Object(); // 1
NewClass tc = new NewClass(); //2
tc.doSomething(obj); //3
obj = new Object(); //4
obj = null; //5
tc.doSomething(obj); //6
}
}
Am I right? I mean, the second time line 7 is evoked is the exact moment where the object created in line 1 is eligible for garbage collector, right?
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
- 
				qazwsx922
 - Posts: 7
 - Joined: Sat Sep 04, 2021 8:17 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.920 :
How do I know line5 makes obj(on line 4) null?,instead of line 1 obj?
I thought line 5 made line1 obj null.
			
			
									
									
						I thought line 5 made line1 obj null.
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.920 :
The code given in the question is:
obj is being reassigned a new Object at //4, that means the statement obj = null; at //5 will cause that Object to lose the reference.
			
			
									
									
						Code: Select all
      Object obj = new Object(); // 1
      NewClass tc = new NewClass(); //2
      tc.doSomething(obj); //3
      obj = new Object();    //4
      obj = null;    //5
      tc.doSomething(obj); //6
Who is online
Users browsing this forum: Bing [Bot] and 10 guests