在该类中声明一个类的实例 [英] Declaring an instance of a class inside that class

查看:126
本文介绍了在该类中声明一个类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在运行时显示错误:

This code shows error at run time:

class Animal {
    Animal object1 = new Animal();

    public static void main(String[] args) {     
        Animal obj = new Animal();
    }
}

这与 Animal obj = new Animal(); 来自主
方法的这一行被注释掉了。此代码显示没有运行时错误。

This is slightly different code with Animal obj = new Animal(); this line from main method commented out. this code shows no run time error.

class Animal {
    Animal object1 = new Animal();

    public static void main(String[] args) {     
        // Animal obj = new Animal();
    }
}

这是如何引起的,我该如何解决?我正在使用命令提示符来运行此代码。

How is this caused and how can I solve it? I am using command prompt to run this code.

推荐答案

如果您有一个成员变量,该变量初始化为相同的实例class,然后在创建该实例时,它还将有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为相同的实例class,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为相同的实例。 class,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为相同的实例。 class,当创建该实例时,它也将有一个成员初始化为同一个类的实例的变量,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有成员初始化为同一个类的实例的变量,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有成员初始化为同一个类的实例的变量,并且当创建该实例时,它还将具有一个成员变量,该变量被初始化为同一个类的实例,并且当创建该实例时,它还将具有成员初始化为同一个类的实例的变量,并且在创建该实例时,它还将具有一个成员变量,该变量初始化为同一个类的实例,并且在创建该实例时,它还将具有成员变量初始化为同一个类的实例...

If you have a member variable which is initialized to an instance of the same class, then when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class...

然后堆栈将溢出并且它将停止。对象有一个指向同一个类的另一个实例作为成员的指针是可以的,但是在构造函数中创建该实例,或者在类的主体中初始化它是不行的,或者你将以递归方式创建对象直到你的堆栈溢出。通常,如果您想要这样的成员变量,那么您接受该对象作为构造函数参数。

and then the stack will overflow and it will stop. It's OK for an object to have a pointer to another instance of the same class as a member, but it's not OK to create that instance in the constructor, or have it initialized in the body of the class, or you'lll recursively create objects until your stack overflows. Normally if you want such a member variable, then you accept the object as a constructor argument.

这篇关于在该类中声明一个类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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