静态引用非静态字段Java错误
首先我要说的是,我一直是 Stack Overflow 的忠实粉丝,在过去的几个学期里,我通常不需要真正提出问题就能找到所有问题的答案。但是,我在使用堆栈程序时遇到了问题。还有比这更多的代码,但我想我已经将问题范围缩小到了这个错误。它指出
线程“main”java.lang.Error中出现异常:未解决的编译 问题:
<块引用>无法对非静态字段 Stack1 进行静态引用
构造函数 Stack(int) 未定义在 stack.main(stack.java:11)
谁能解释一下问题可能是什么?或者更好的是,指出我可以解释它的地方?我尝试在 Overflow 上和谷歌上查找它,但我认为不知道我实际在寻找什么和/或疲劳的结合使我无法找到具体的答案。感谢您提前提供的任何帮助。
public class stack {
private Object[] Stack1;
private int topOfStack;
private int max;
//private int empty;
//private int capacity;
public static void main(String[] args) {
Stack1 = new Stack(5);
}
public Stack(int size) {
if (size < 0){
throw new IllegalArgumentException("Parameter must be >0. Parameter was " + size + ".");
}
max = size;
Stack1 = (Object[]) (new Object[size]);
topOfStack = -1;
}
}
I'll preface this by saying that I've been a longtime fan of Stack Overflow, and over the past few semesters I've usually been able to find the answer to all my questions without actually asking one. However, I've been having problems with a stack program. There's more code than this, but I think I've narrowed my problem down to this one error. It states
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:Cannot make a static reference to the non-static field Stack1
The constructor Stack(int) is undefinedat stack.main(stack.java:11)
Can anyone explain what the issue might be? Or better yet, point me to somewhere that will explain it? I've tried looking it up on Overflow and through google, but I think a combination of not knowing what I'm actually looking for and/or fatigue is preventing me from finding a concrete answer. Thanks for any help in advance.
public class stack {
private Object[] Stack1;
private int topOfStack;
private int max;
//private int empty;
//private int capacity;
public static void main(String[] args) {
Stack1 = new Stack(5);
}
public Stack(int size) {
if (size < 0){
throw new IllegalArgumentException("Parameter must be >0. Parameter was " + size + ".");
}
max = size;
Stack1 = (Object[]) (new Object[size]);
topOfStack = -1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将您的类重命名为“Stack”(大写)并将行:更改
为:
Rename your class "Stack" (uppercase) and change the line:
to:
在下面的行中 -
Stack1
不是静态。在下面,您尝试
从
main
中引用Stack1
,它是静态。因此 -
您的构造函数名称是
Stack
,它与类名称stack
不匹配。所以 -可能您想将您的类命名为 Stack,并在您的 main 中添加以下内容 -
In the following line -
Stack1
is not static.And in the following you are trying to refer to
Stack1
-from within
main
which is static.Thus -
Your constructor name is
Stack
which does not match class name isstack
. So -May be, you want to name your class
Stack
and in yourmain
the following -您的类被声明为小写的
stack
。Your class is declared as
stack
lowercase.保留小写的类声明,我猜分配
不是你的意思。相当:
Keeping the lower case class declaration, I'm guessing the assignment
is not what you meant. Rather:
Stack1
是stack
类的实例变量。在静态 main 方法中,您尝试将值存储在 Stack1 变量中,即使它是实例变量。另外,您尝试将非数组类型分配给类型为Object
的数组的变量。此外,即使该类以小写形式称为stack
,您也有一个Stack
的构造函数(注意大写)。Stack1
is in instance variable to thestack
class. In your static main method, you try to store a value in theStack1
variable even though it is an instance variable. Also, you try to assign a non-array type to a variable whose type is an array ofObject
. Furthermore, you have a constructor forStack
(note the uppercase) even though the class is calledstack
in lowercase.