在Java中构造新对象时出现堆栈溢出错误
我正在尝试构造一个具有一组特定值的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次创建新对象都会导致另一个新对象的创建(依此类推...),直到堆栈溢出。
相反,它应该看起来像这样:
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:
您在构造函数中进行了递归调用。将构造函数保留为空(只需将其删除)并从
main
方法运行此代码:You have a recursive call in the constructor. Leave the constructor empty (simply delete it) and run this code from
main
method:构造函数递归调用自身,导致堆栈溢出。
Constructor recursively calls itself, causing stackoverflow.
您在
new
的处理程序中调用new
,创建了一个无限循环(并且由于堆栈是有限的,它最终会耗尽空间)。但是public PackingCase() { ... }
是一个构造函数。这意味着只有当有人已经使用了new PackingCase()
时才会调用它。构造函数中的代码不必创建对象(分配空间),只需初始化它(为其字段设置值)。You are calling
new
within the handler fornew
, creating an infinite loop (and since the stack is finite, it eventually runs out of space). Butpublic PackingCase() { ... }
is a constructor. That means it is only called when someone has already usednew PackingCase()
. The code within the constructor doesn't have to create the object (allocate space), just initialize it (set values for its fields).