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
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.920 :
If you like our products and services, please help us by posting your review here.
-
- 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!
-
- 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?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.920 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- 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.
-
- Site Admin
- Posts: 10090
- 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
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 6 guests