kumar shiva wrote:
Why compile error occurs at a1 object creation(2nd line)
There are no compiler error in that code.. Rather a StackOverflowError.
Notice that every instance of you class Example has a new instance of the same class..
This becomes something like this: -
a2 (has) a1 (has) a1 (has) a1 (has) a1 (has) a1 (has) a1 ........... and so on...
So, every instance of Example has another instance of Example, and it keeps going, until StackOverflowError in reached..
*NOTE:- All the a1's above are not the same, they are different instance pointing to different objects.. Since they are instance reference..