在Java中构造新对象时出现堆栈溢出错误

发布于 2024-11-26 05:41:48 字数 1004 浏览 3 评论 0原文

我正在尝试构造一个具有一组特定值的 PackingCase 对象。虽然程序在编码期间没有显示错误,但在运行时,我收到此错误;

Exception in thread "main" java.lang.StackOverflowError
at assignment.pkg2.PackingCase.<init>(PackingCase.java:59)
at assignment.pkg2.PackingCase.<init>(PackingCase.java:60)

我的代码如下;

public class PackingCase {
// private fields go here

int serialNumber;
int timesUsed;
int timeCreated;
int timeStored;
String name;
String description;


void setCase(int s, int TU, int TC, int TS){
    serialNumber = s;
    timesUsed = TU;
    timeCreated = TC;
    timeStored = TS;

}

double volume(){
    return serialNumber*timesUsed*timeCreated*timeStored;
}

public PackingCase(){ 
    PackingCase PC1 = new PackingCase();
    double vol;

    PC1.setCase(1, 2, 3, 4);

    vol = PC1.volume();
    System.out.println(""+vol);




}

第 59 行是“public PackingCase(){”,第 60 行是“PackingCase PC1 = new PackingCase();”。我不知道发生了什么,考虑到我发现的一个示例使用几乎相同的代码结构,并且编译时没有任何错误。任何帮助将不胜感激。

I'm trying to construct a PackingCase object with a certain set of values. While the program shows no errors during coding, when running, I get this error;

Exception in thread "main" java.lang.StackOverflowError
at assignment.pkg2.PackingCase.<init>(PackingCase.java:59)
at assignment.pkg2.PackingCase.<init>(PackingCase.java:60)

My code is as follows;

public class PackingCase {
// private fields go here

int serialNumber;
int timesUsed;
int timeCreated;
int timeStored;
String name;
String description;


void setCase(int s, int TU, int TC, int TS){
    serialNumber = s;
    timesUsed = TU;
    timeCreated = TC;
    timeStored = TS;

}

double volume(){
    return serialNumber*timesUsed*timeCreated*timeStored;
}

public PackingCase(){ 
    PackingCase PC1 = new PackingCase();
    double vol;

    PC1.setCase(1, 2, 3, 4);

    vol = PC1.volume();
    System.out.println(""+vol);




}

Line 59 is "public PackingCase(){" , and Line 60 is "PackingCase PC1 = new PackingCase();". I have no idea what's going on, considering that an example I found uses virtually the same code structure, and compiles with no errors whatever. Any help would be appreciated.

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

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

发布评论

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

评论(4

泛滥成性 2024-12-03 05:41:48

每次创建新对象都会导致另一个新对象的创建(依此类推...),直到堆栈溢出。

相反,它应该看起来像这样:

public PackingCase(){ 
    this.setCase(1, 2, 3, 4);
    vol = this.volume();
    System.out.println(""+vol);
}

Each creation of a new object leads to the creation of another new object (and so on...) until the stack is overflowed.

Instead, it should be look like that:

public PackingCase(){ 
    this.setCase(1, 2, 3, 4);
    vol = this.volume();
    System.out.println(""+vol);
}
揽清风入怀 2024-12-03 05:41:48

您在构造函数中进行了递归调用。将构造函数保留为空(只需将其删除)并从 main 方法运行此代码:

public static void main(String[] a){
    PackingCase pc1 = new PackingCase();
    pc1.setCase(1, 2, 3, 4);
    double vol = pc1.volume();
    System.out.println(""+vol);
}

You have a recursive call in the constructor. Leave the constructor empty (simply delete it) and run this code from main method:

public static void main(String[] a){
    PackingCase pc1 = new PackingCase();
    pc1.setCase(1, 2, 3, 4);
    double vol = pc1.volume();
    System.out.println(""+vol);
}
假装爱人 2024-12-03 05:41:48
public PackingCase(){      PackingCase PC1 = new PackingCase(); ...}

构造函数递归调用自身,导致堆栈溢出。

public PackingCase(){      PackingCase PC1 = new PackingCase(); ...}

Constructor recursively calls itself, causing stackoverflow.

<逆流佳人身旁 2024-12-03 05:41:48

您在 new 的处理程序中调用 new,创建了一个无限循环(并且由于堆栈是有限的,它最终会耗尽空间)。但是 public PackingCase() { ... } 是一个构造函数。这意味着只有当有人已经使用了 new PackingCase() 时才会调用它。构造函数中的代码不必创建对象(分配空间),只需初始化它(为其字段设置值)。

You are calling new within the handler for new, creating an infinite loop (and since the stack is finite, it eventually runs out of space). But public PackingCase() { ... } is a constructor. That means it is only called when someone has already used new PackingCase(). The code within the constructor doesn't have to create the object (allocate space), just initialize it (set values for its fields).

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