New object of class inside the class

2019-03-07 17:48发布

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.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-07 18:39

It will lead to Stack Overflow Error.

查看更多
做个烂人
3楼-- · 2019-03-07 18:41

Yes.

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

查看更多
登录 后发表回答