类内部的新对象 [英] New object of class inside the class

查看:94
本文介绍了类内部的新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java或其他OOP语言中 -

In Java, or other OOP languages-

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

  void mymeth()
  {
  }
}


b $ b

这里,在类中创建一个类的对象。当新对象在里面创建时,它将创建新的成员和类对象(b这里),这将再次创建成员,对象?这不会使链导致无限的对象和变量吗?
Dumb查询。

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.

推荐答案

是的。



遇到 StackOverflowError (讽刺,不是吗?)如果你尝试这样做。

Yes.

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

这篇关于类内部的新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆