Suppose we have a class named Main.
Which of the following statements are true?
Choose all that apply
1: public class Main { 2: public static void main(String[] args) { 3: Main one = new Main(); 4: Main two = new Main(); 5: Main three = one; /*from w w w .jav a2s.com*/ 6: one = null; 7: Main four = one; 8: three = null; 9: two = null; 10: two = new Main(); 11: System.gc(); 12: } 13:}
B, D.
The Main object from line 3 has two references to it: one and three.
The references are nulled out on lines 6 and 8, respectively.
Option B is correct because this makes the object eligible for garbage collection after line 8.
Line 7 sets the reference four to the now null one, which means it has no effect on garbage collection.
The Main object from line 4 only has a single reference to it: two.
Option D is correct because this single reference becomes null on line 9.
The Main object declared on line 10 becomes eligible for garbage collection at the end of the method on line 12.
Calling System.gc()
has no effect on eligibility for garbage collection.