-1

I didn't know how to phrase this question, and I'm not asking for help, I'm just curious as to why this is possible.

Say I have a class foo:

public class foo {

    private foo i = this;

    public foo() {
        ...
    }
}

Why am I able to set i to an instance of the class itself?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ddlkkd
  • 25
  • 1
  • 6
  • 3
    Why shouldn't you be able to do so? `i` is a variable of type `foo`, this means that it can hold a reference to an object of the same type. `this` is an object of type `foo`, so you can assign it to `i`. – BackSlash Aug 22 '18 at 12:45
  • 1
    `i` and `this` are just references (much like pointers in languages like C++), i.e. they will "point" to the same instance. Nothing odd here. – Thomas Aug 22 '18 at 12:46
  • 1
    How would a Singleton be implemented if `getInstance()` couldn't return an instance of the class itself? – deHaar Aug 22 '18 at 12:46
  • the foo in `foo i` is just a type. (please name your class accordingly, with `Foo`). You can have a class pointing to an(other) instance of the same class. – KarelG Aug 22 '18 at 12:46
  • Maybe are you asking in relation to the execution of the constructor? – Thiyagu Aug 22 '18 at 12:48
  • I think it was weird to me that a class could reference itself in a variable, rather than it being referenced outside the class like normal. I'm just figuring out the quirks of Java at this point, thanks. – ddlkkd Aug 22 '18 at 12:50
  • @deHaar [Like this](https://ideone.com/p1dDH2) ? – Michael Aug 22 '18 at 12:50
  • @Michael `static`, sure… But it is still some kind of class attribute, isn't it? – deHaar Aug 22 '18 at 12:55
  • @deHaar From my comprehension, OP is only confused as to why an *instance* might want to store a reference to itself as a field. The example you've given does not prove that it's necessary to allow such behaviour, as I've demonstrated. – Michael Aug 22 '18 at 12:58
  • @Michael Yeah, I'm relatively new to Java and I didn't see the use in a class storing a reference to itself, when other classes would usually be using instances of it instead. – ddlkkd Aug 22 '18 at 13:00
  • @Michael ok, you're right then… – deHaar Aug 22 '18 at 13:02
  • @ddlkkd we have only one object in heap and `this` and `i` both refers to it. You can see my answer below for details – nits.kk Aug 22 '18 at 13:05

2 Answers2

1

Variables in Java are like pointers, they are not actually the object, but point to the storage where the object is saved. So you create a pointer, pointing to your own object.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
Donatic
  • 323
  • 1
  • 13
0

Lets see what would happen when an object of foo is created.

foo x = new foo();

On encountring new JVM will create an object of foo in heap. Next step (Note its Next step) is constructor execution. In java object is created on heap and then constructor is invoked. This is the reason even if constructor throws an exception, even then object is created (can be reclaimed in finalize()). As object is already created and is present in heap references can refer to it. this is reference to the object.

Now private foo i = this; is executed as a part of constructor (this is as per java behavior, all instance fields if initialized at the place of declaration, it is executed whenever constructor is invoked). this already refers to the object created on heap and now i also refers to the same Object on Heap. Once constructor is successfully executed without any exception the variable x also refers to the same Object on heap.

So we have one object of foo and we have references to that object as below :

  1. this (from within instance methods of foo)
  2. we have instance vatiable i
  3. and we have variable x as reference to that Object

Also as i is of type foo so it can refer to an object of type foo and this is also a reference of type foo referring to an object of type foo. Making i equal to this makes i also referring to same object.

Hope this helps in clearing your doubts.

nits.kk
  • 5,204
  • 4
  • 33
  • 55