Welcome to JavaRanch. Please
UseCodeTags when you post source code.
Do you understand what happens if you have code like this:
(you have exactly the same construct in class Q).
What happens when you create a new P object?
1. A new P object is created.
2. The object is initialized. It has a member variable p1, for which a new P object needs to be created.
3. That new P object is created.
4. But it also needs to be initialized. It has its own member variable p1, for which a new P object needs to be created.
5. Go to step 3.
So, there is an infinite recursive loop which keeps running until the stack overflows, and then you get a StackOverflowError.