SCJP 1.6版考題 120
出自 陳富國維基館
Given: 1. public class GC{ 2. private Object o; 3. private void doSomethingElse(Object obj){o = obj;} 4. public void doSomething(){ 5. Object o = new Object(); 6. doSomethingElse(o); 7. o = new Object(); 8. doSomethingElse(null); 9. o = null; 10. } 11.} When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection? A. Line 5 B. Line 6 C. Line 7 D. Line 8 E. Line 9 F. Line 10
解答
Ans: D
解說:
方法變數o指向第5建立的物件在第6行方法doSomethingElse執行時,被GC物件的o參考, 即使第8行方法變數o指向新的物件也還是被參考住而不是垃圾物件的候選, 直到再次呼叫doSomethingElse方法,將GC物件的o設為null,第5行建立的物件就沒有指向。
|