Java Object reference and garbage collection

42 Views Asked by At

I'm currently studying towards my Java associate certificate and I have the official textbook for study. In the text book is the following example code.

public class Island {
    Island i;

    public static void main(String[] args) {

        Island i2 = new Island();
        Island i3 = new Island();
        Island i4 = new Island();

        i2.i = i3;
        i3.i = i4;
        i4.i = i2;

I understand here that I have initialised 3 Island objects with reference variables i2, i3 and i4 which each point to their own object. i2 is then redirected to point towards i3 and i3 towards i4 etc. What I don't understand is the need for the "i2.i" dot operator, what exactly is it doing here ? is i2 = i3 not as equally valid ?

If anyone has any good resources on where I can read quite in depth into all of the applications of the dot operator in java that would also be helpful, thanks.

2

There are 2 best solutions below

3
ssc-hrep3 On

You're using an instance variable of the same type as the class itself (see line Island i). This means, that the class Island holds an attribute i of the same type Island. Every island has therefore a link to another island. Your assignment of i2.i = i3; defines the instance variable of i2 to be i3. In other words: i2 has a link to i3. You can get i3 if you have i2.

If you used the assignment i2 = i3, the value of i2 would be overridden by the reference of i3. This means that i2 is not used anymore and the object behind i3 would also be the same object behind i2 (same object, 2 different variable names).

1
Kerrek SB On

It's important to be careful with the details here.

You have indeed created three objects, but the objects are distinct from the variables.

Let's simplfy: Consider Island x = new Island();, and Island y; You have two variables, x and y, but only one object. The object doesn't have a name, but it is bound to the variable x, so when you say x, you get that object. And when you say y, you get nothing (y is null).

The dot accesses the object denoted by by the expression that precedes it. So x.i accesses the i member variable of the object that is bound to x, and similarly, y.i is attempting to access a member-variable of no object at all, which causes an exception to be thrown.

So now it is clear that you can say x.i = x; to set the member variable Island.i of the object bound to x to the value that happens to be the same object. Or you could set it to something else, like x.i = new Island(); or x.i = y;.

The dot doesn't have to be preceded by a variable, any expression will do. For example, you could say (new Island()).i = x; to create a new object (which again doesn't have a name; objects never have names) and bind the i member of that object to the object bound to x. Since this object is never bound to any variable, it is immediately eligible for collection.

The point of your code example is that all objects are bound to variables that exist beyond the scope of the i1, i2 and i3 (namely to the member variables of the three objects), and thus they form a reference cycle. An interesting question on the topic of garbage collection is whether the three objects are eligible for collection.