Instantiate an instance inside its class
I always read codes like following:
Class A {
A a = new A( );
}
Could anybody tell me why a class is able to instantiate its instance inside itself.
Another example:
Class A {
B b = new B( );
}
Class B{
A a = new A( )
}
When Class A is running, it calls Class B, but when Class B is instantiated
, it needs to call Class A. I am always puzzled about this kind of codes.
[454 byte] By [
Flashgeta] at [2007-10-2 22:43:44]

Why should this be suprising?
Your "pseudo" code doesn't show the context in which the instantiations appear, but unless you try to do this inside the constructor, there is nothing special about creating an instance of the same class. Inside the constructor, you have to take care not get infinite recursion.
Everything in the world is an object - not only the building, but also the blueprint for this building is some kind of sheet of paper.
In Java, the Class Objects are loaded as soon as you go into runtime. The VM recognizes all necessary classes, and creates one single instance of them. So, static attributes are indeed a singleton pattern on class level...
A class even has an own constructor. This constructor looks like this:
class ClassA{
{
// Insert Class Constructor input here
}
}
All static attributes are implicitly constructed in this class constructor.
However, what you have written, is just a simple Form of:
class ClassA{
ClassA a;
public ClassA(){
a = new ClassA();
}
}
This is indeed a neverending loop, so this doesn't make sense. But it isn't unusual to hold a reference to another object of the same type.