How can a class have a member of its own type, isn't this infinite recursion?
You're only declaring the variable and not creating it. Try creating it at declaration or in the constructor and let me know what happens:
public class Abc {
private Abc p = new Abc(); // have fun!
public static void main(String[] args) {
new Abc();
}
}
Incidentally, if you don't create it in the class, but rather accept a reference to it in a getter method or a constructor parameter, your code will work just fine. This is how some linked lists work.
The difference lies in compile-time vs run-time checks.
In the first case (compile time), you are declaring that you will have a reference to a value of type Abc
in this instance. The compiler will be aware of this when it checks for proper semantics, and since it knows the type upon compile time, it sees no issue with this.
In the second case (run time), you will actually create a value for this reference to refer to. This is where you could potentially get yourself into trouble. For example, if you said the following:
public class Abc {
private Abc p;
public Abc() {
p = new Abc();
}
}
This could lead you into trouble for the exact reason you cited (recursion that contains no base case and will continually allocate memory until you've run the VM out of heap space).
However, you can still do something similar to this and avoid the infinite recursion. By avoiding creating the value during construction, you put it off until a method is called for. In fact, it's one of the common ways to implement the singleton pattern in Java. For example:
public class Abc {
private Abc p;
private Abc() { // Private construction. Use singleton method
}
public static synchronized Abc getInstance() {
if (p == null)
p = new Abc();
return p;
}
}
This is perfectly valid because you only create one new instance of the value, and since run-time will have loaded the class already, it will know the type of the instance variable is valid.
When you want to model some real-world scenarios, you might have to use this notion. For example, think of a Tree's branch. A tree's branch might have n number of branches on it. Or from computer science background, think of a linked list's Node. A node will have reference to the node next to it. At the end, the next will contain a null to indicate end of the list.
So, this is only a reference, indicating that this type might refer to one of it's own. Nothing more.
As the chosen answer states, it's okay because the variable is not being instantiated, but rather it is accepting a reference from a set method or constructor argument. So you see, it's just a reference, just an address.
However though, there is a way of instantiating it without causing the nightmare loop, and that's by declaring it as static. This sort of bends the recursion rule because a static member is not on an object level but on a class level.
So...
public class Abc
{
public static Abc p = new Abc ();
}
Works just fine because the variable 'p' is not really affected by the instantiation of the Abc
class. It's almost the equivalent of creating the 'p' object in another class.
Remember that I can do this...
public class Abc
{
public static Abc p = new Abc ();
public static void main(String [] args)
{
Abc.p;
}
}
Without creating a new object of Abc in the main method. That's how statics work, they're virtually unaffected by instantion of objects, and in the case of your question, they are the exception to the recursion rule.
Summarizing some of the answers here.
The class contains reference to one of its own kind.The best analogy in real world would be of a treasure hunt. A Clue place has some data and a clue in it which leads to another clue place which again you know repeats.
It is not saying that a clue place has another clue place in it, the kind of which you are seeming to infer.