Suppose we have a class named Main. Which of the following statements are true?
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; 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.
B is correct because this makes the object eligible for garbage collection after line 8.
The Main object from line 4 only has a single reference to it: two.
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.