New object of class inside the class

2019-03-07 18:26发布

问题:

In Java, or other OOP languages-

class MyClass{
  int a=5;
  MyClass b=new MyClass();

  void mymeth()
  {
  }
}

Here, creating an object of the class inside the class. When new object is created inside, it will create new members and class object(b here), which will again create members, objects? Won't this make a chain leading to infinite objects and variables? Dumb query.

回答1:

Yes.

You run into a StackOverflowError (ironic, isn't it?) if you try to do that.



回答2:

It will lead to Stack Overflow Error.