简体   繁体   English

初始化新对象时的StackOverflow

[英]StackOverflow when initializing new object

I have no idea why is this happenning, it seems to be in a constant loop between creating new object and initializing it 我不知道为什么会这样,它似乎在创建新对象和初始化它之间处于不断循环中

public class App {

Character hero = new Character();
charCreation charCreation = new charCreation();

public static void main(String [] args){
    App app = new App();
    app.run();
}
    void run(){
        charCreation.charCreate();

    }




}

So here App creates a new charCreation object which is this 因此,这里App创建了一个新的charCreation对象,这是

import java.util.Scanner;

class charCreation extends App {

Scanner skan = new Scanner(System.in);

protected  void charCreate(){

......

And here's the error 这是错误

Exception in thread "main" java.lang.StackOverflowError 线程“主”中的异常java.lang.StackOverflowError

at charCreation.(charCreation.java:3) 在charCreation。(charCreation.java:3)

at App.(App.java:5) 在App。(App.java:5)

at charCreation.(charCreation.java:3) 在charCreation。(charCreation.java:3)

at App.(App.java:5) 在App。(App.java:5)

...... ......

it goes on and on 它会一直持续下去

When you create an instance of CharCreation (fixed CamelCase for you), it will also initialize everything inherited from its superclass App (calling the superclass constructor, initializing all the instance fields, etc). 当您创建CharCreation的实例(为您固定的CamelCase)时,它还将初始化从其超类App继承的所有内容(调用超类构造函数,初始化所有实例字段等)。 Part of that is creating another instance of CharCreation (as well as an instance of Character ). 其中一部分是创建CharCreation另一个实例(以及Character的实例)。

Infinite loop. 无限循环。

Maybe you want to remove that instance field from App and make it a local variable in run instead. 也许您想从App删除该实例字段,并使其在run时成为局部变量。

The charCreation class extends App so to create it it needs to call the constructor of the superclass ( App ). charCreation类扩展了App因此要创建它,需要调用超类的构造函数( App )。 To construct the App superclass, the fields of App need to be costructed, including a new charCreation() - and so it loops. 为了构建App超,领域的App需要被costructed,包括一个new charCreation() -因此它循环。

You need to decouple creation of a new charCreation object from the creation of the App instance. 您需要将新charCreation对象的创建与App实例的创建分离。

You have an App class with a field of type CharCreation . 您有一个App类,其字段类型为CharCreation

CharCreation extends App , so when you initialize a CharCreation object, it will initialize the field charCreation . CharCreation扩展了App ,因此,当您初始化CharCreation对象时,它将初始化字段charCreation And so, when the charCreation field is initialized, it will initialize a new CharCreation object, and so on. 因此,当初始化charCreation字段时,它将初始化一个新的CharCreation对象,依此类推。

It's basically a design issue you have to solve, and I think you CharCreation class shouldn't extends the App class, and charCreate should return a Character . CharCreation ,这是一个必须解决的设计问题,我认为CharCreation类不应扩展App类,而charCreate应该返回Character

public class App {
    Character hero = new Character();
    CharCreation charCreation = new charCreation();

    void run(){
        hero = charCreation.charCreate();

    }
}

public class CharCreation {
   public Character charCreate() {
       /* creates and returns the hero */
   }
}

Side note: 边注:

  • Always name your classes with a first upper letter to match the java convention, CharCreation and not charCreation . 始终使用第一个大写字母命名您的类,以匹配Java约定CharCreation而不是charCreation
  • Don't use Character to name a class, it's a class which already exist in the JDK (the object counterpart of char ). 不要使用Character来命名一个类,它是JDK( char的对象对应物)中已经存在的一个类。

Cyclic object creation which caused stackoverflow. 循环对象的创建导致堆栈溢出。

  1. App object create charCreation instance object 应用程序对象创建charCreation实例对象

  2. charCreation object will also create App Object as it is super class.[super class object instantiate before child] charCreation对象还将创建应用程序对象,因为它是超类。[超类对象在子级之前实例化]

  3. Cycle found, go on... till the stackoverflow. 找到循环,继续...直到stackoverflow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM