wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes G.C. on local variable......... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "G.C. on local variable........." Watch "G.C. on local variable........." New topic
Author

G.C. on local variable.........

Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";//local variable
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
f) it is hard to say whether After line 11 or line 12
why the answer is e,not f!


a beginner in java
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
it is because the content of variable anObject gets out of scope at line 11!!
the answer could be line 09 only if locObj wasn't assigned for anObject's content!!!
the content of a variable becomes subject of garbage collection if there is no more ways to achieve it!!! In this case there will remain a path to the content that was first assigned to anObject by the local var locObj, when locObj gets out of scope, there will be no more reachable paths!!! hope it helps!!! Correct me if wrong!!!
Igor Zeta
Ranch Hand

Joined: Nov 12, 2002
Posts: 40
I'm agree with you
in fact the var is defined
local to the if-block
so its scope ends when the block ends.


ciao<br />Igor Zeta<br />SCJP1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In the exam there will not be questions about the garbage collection eligibility of objects either:
a) pointed to by local variables that falls out scope
or
b) string objects computed from string literals
Please trust me


SCJP2. Please Indent your code using UBB Code
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Originally posted by Jose Botella:
In the exam there will not be questions about the garbage collection eligibility of objects either:
a) pointed to by local variables that falls out scope
or
b) string objects computed from string literals
Please trust me

Jose
While, i do agree with your point b), I have had questions on local reference variables and GC when I took the exam
Sri
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I guess it was not a question about falling out of scope, but something about aliasing the same object with several variables; and maybe setting some to null ???
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hi everybody,
I am wondering about the trim methods associated with the objects.
What is their purpose.
Thank you
Pallavi
Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
Originally posted by Mellihoney Michael:
At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";//local variable
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
f) it is hard to say whether After line 11 or line 12
why the answer is e,not f!


Is "sample" created in the pool or on the heap?


go...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: G.C. on local variable.........