I am preparing for OCPJP, and I got stuck at the following mock exam question:
Given:
3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println("whinny"); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println("vinny"); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
12. Icelandic i3 = new Icelandic();
13. i3 = i1; i1 = i2; i2 = null; i3 = i1;
14. }
15. }
When line 14 is reached, how many objects are eligible for the garbage collector?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6
Their correct answer is E, i.e. four objects, but I’m not sure why. From my point of view, i2 and its weight will get eligible for garbage collection. Perhaps I’m missing something, please advise.
Lets call
Icelandic()on line 11IceA, line 12IceB, and so forth.After creation
After
i3 = i1After
i1 = i2After
i2 = nullAfter
i3 = i1So only the
Icelandic()created on line 12 remains. Now, eachIcelandic()has aLong weight, soIceAandIceCare now unreferenced, meaning 4 objects (IceA,IceA.weight,IceC,IceC.weight) are available for GC.Other issues:
argsis stillargs, they are not counting going out of scope in this questionLong weightis not declared statically, so each instance of the class has aweightobject.