-2
class Eco {
    Eco e;

    public static void main(String[] args){
        Eco e1 = new Eco();
        Eco e2 = new Eco();
        Eco e3 = new Eco();

        e3.e = e2;
        e1.e = e3;
        e2 = null;
        e3 = null;
        e2.e = e1;
        e1 = null;
    }

}

Could someone visualize how the objects interact? This is a mocking question for my upcoming OCA exam and even if I do understand this topic, I still have some trouble actually visualizing what is actually happening. I did find the Error that happens if we reach e2.e = e1; since e2 was already null. I hope I made myself clear enough to understand what my question is :). Is it more like this:

[e1] --------> [e1 Object] ---------> [e Object]

[e2] --------> [e2 Object] ---------> [e Object]

[e3] --------> [e3 Object] ---------> [e Object]

or is it more like:

[e1] --------> [e1 Object|e Object]

[e2] --------> [e2 Object|e Object]

[e3] --------> [e3 Object|e Object]

I really hope I made myself clear here :D

let me know if I could be more specific.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Loskyll
  • 13
  • 5

1 Answers1

0

The code

e3.e = e2;
e1.e = e3;

gives you linking like this e1.e -> e3.e -> e2 Making e2 and e3 null doesn't make them eligible for garbage collection cos they are raceable through e1.
only making e1=null makes all of them eligible for garbage collection.
but the code throws NullpointerException at e2.e = e1, which is the right answer.

the Hutt
  • 16,980
  • 2
  • 14
  • 44