There is 1 object eligible for Garbage Collection just before the main method ends.
Initially, you create one object:
X x = new X(); //One object on the heap assigned to 'x'
Then you call the 'fun' method which creates another object, this is returned and assigned to X2
X x2 = fun(x); //Two objects on the heap one assigned to 'x' and one to 'x2'
Then, yet another is created and assigned to x4...
X x4 = new X(); //Three objects on heap, one assigned to 'x', one assigned to 'x1', one to 'x4'
So, at this point, you have 3 different objects, but then you point x2 to the same object that x4 points to:
x2 = x4;
That means you now have 1 object to which 2 variables are pointing. This leaves the 2nd object that was created, with no reference. This object is eligible for GC