创建本地和实例对象时出现 java StackOverflowError

发布于 2024-09-18 10:35:58 字数 416 浏览 5 评论 0原文

大家好,有人可以解释一下为什么这段代码会给我 StackOverflowError 错误吗? 如果您能解释 instanceObj 初始化并调用 ObjectTest 构造函数和 java.lang.Object 构造函数时发生的情况,我真的很感激。在我看来,ObjectTest 构造函数一遍又一遍地循环。但我不知道确切的原因?所以任何建议...


public class ObjectTest {

  public ObjectTest() {

   }


  ObjectTest instanceObj = new ObjectTest();


  public static void main(String[] args) {

     ObjectTest localObj = new ObjectTest();
   }
}

Hi can anybody please explain me why is this code snippet giving me StackOverflowError
I really appreciate if you can explain what is happening when instanceObj initializing and calling ObjectTest constructor and java.lang.Object constructor. It seems to me ObjectTest constructor loop over and over.But I don't know exact reason? So any suggestion...


public class ObjectTest {

  public ObjectTest() {

   }


  ObjectTest instanceObj = new ObjectTest();


  public static void main(String[] args) {

     ObjectTest localObj = new ObjectTest();
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝情姑娘 2024-09-25 10:35:58

您调用构造函数来创建对象的新实例。它有对另一个实例的引用,您可以使用另一个新的 ObjectType 实例来初始化该实例,而该实例又会执行相同的操作。在出现该错误之前,需要进行无数次调用。

这会起作用。

public class ObjectTest { 

  public ObjectTest() { 

   } 


  public static void main(String[] args) { 

     ObjectTest localObj = new ObjectTest(); 
   } 
} 

You call the constructor to create a new instance of your object. It has a reference to another instance, which you initialize with another new instance of ObjectType which, in turn, does the same thing. it's an infinite number of calls until you get that error.

This will work.

public class ObjectTest { 

  public ObjectTest() { 

   } 


  public static void main(String[] args) { 

     ObjectTest localObj = new ObjectTest(); 
   } 
} 
寻找一个思念的角度 2024-09-25 10:35:58

让我们看看将执行什么:

  1. main() 创建一个 ObjectTest 的新实例,
  2. ObjectTest 类有一个字段 instanceObj 其中将包含一个 ObjectTest
  3. 其中使用新的 ObjectTest 初始化的 instanceObj
  4. 转到步骤 2

我认为您想要更像这样的东西:

public class ObjectTest {
    private static ObjectTest instanceObj;

    private ObjectTest() {
    }

    public static ObjectTest getInstance() {
        if (instanceObj != null) {
            instanceObj = new ObjectTest();
        }
        return instanceObj;
    }

    public static void main(String[] args) {

        ObjectTest localObj = ObjectTest.getInstance();
    }
}

或者这个:

public class ObjectTest {
    private static final ObjectTest instanceObj = new ObjectTest();

    private ObjectTest() {
    }

    public static ObjectTest getInstance() {
        return instanceObj;
    }

    public static void main(String[] args) {

        ObjectTest localObj = ObjectTest.getInstance();
    }
}

这就是单例模式。


关于同一主题:

Let's see what will be executed :

  1. main() create a new instance of ObjectTest
  2. the ObjectTest class has a field instanceObj which will contain an ObjectTest
  3. the instanceObj in initialized with a new ObjectTest
  4. go to step 2

I think you wanted something more like this :

public class ObjectTest {
    private static ObjectTest instanceObj;

    private ObjectTest() {
    }

    public static ObjectTest getInstance() {
        if (instanceObj != null) {
            instanceObj = new ObjectTest();
        }
        return instanceObj;
    }

    public static void main(String[] args) {

        ObjectTest localObj = ObjectTest.getInstance();
    }
}

Or this :

public class ObjectTest {
    private static final ObjectTest instanceObj = new ObjectTest();

    private ObjectTest() {
    }

    public static ObjectTest getInstance() {
        return instanceObj;
    }

    public static void main(String[] args) {

        ObjectTest localObj = ObjectTest.getInstance();
    }
}

This is the Singleton pattern.


On the same topic :

咋地 2024-09-25 10:35:58

每个ObjectTest 实例都引用另一个 ObjectTest — 名为instanceObj。该实例是在创建其“父”实例时构建的……因此导致了另一个、另一个、无穷无尽的构建。

下面是等效代码,可以更清楚地指出该缺陷:

public class ObjectTest {

  ObjectTest instanceObj;

  public ObjectTest() {
    instanceObj = new ObjectTest(); /* Recursively call the constructor. */
  }

}

Every ObjectTest instance refers to another ObjectTest—named instanceObj. This instance is constructed when its "parent" instance is created… and thus results in construction of another, and another, ad infinitum.

Here is equivalent code that may point out the flaw more clearly:

public class ObjectTest {

  ObjectTest instanceObj;

  public ObjectTest() {
    instanceObj = new ObjectTest(); /* Recursively call the constructor. */
  }

}
止于盛夏 2024-09-25 10:35:58

因为你正在递归地创造你自己。

您需要注入实例,或者让其他类管理该属性。

public class ObjectTest {

 public ObjectTest() {
    instanceObj  = null;
 }
 public ObjectTest(ObjectTest myObjectTest) {
     instanceObj = myObjectTest;
 }
}

Because you are recursively creating yourself.

You need to inject your instance, or have some other class manage that property.

public class ObjectTest {

 public ObjectTest() {
    instanceObj  = null;
 }
 public ObjectTest(ObjectTest myObjectTest) {
     instanceObj = myObjectTest;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文