静态引用非静态字段Java错误

发布于 2024-12-08 04:34:47 字数 901 浏览 0 评论 0原文

首先我要说的是,我一直是 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 undefined

at 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 技术交流群。

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

发布评论

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

评论(5

戴着白色围巾的女孩 2024-12-15 04:34:47

将您的类重命名为“Stack”(大写)并将行:更改

Stack1 = new Stack(5)

为:

Stack stack1 = new Stack(5);

Rename your class "Stack" (uppercase) and change the line:

Stack1 = new Stack(5)

to:

Stack stack1 = new Stack(5);
孤檠 2024-12-15 04:34:47

在下面的行中 -

private Object[] Stack1;

Stack1 不是静态

在下面,您尝试

public static void main(String[] args) {
    Stack1 = new Stack(5);
}

main 中引用 Stack1,它是静态

因此 -

无法对非静态字段 Stack1 进行静态引用

您的构造函数名称是 Stack,它与类名称 stack 不匹配。所以 -

构造函数 Stack(int) 未定义

可能您想将您的类命名为 Stack,并在您的 main 中添加以下内容 -

public static void main(String[] args) {
    Stack stack1 = new Stack(5);
}

In the following line -

private Object[] Stack1;

Stack1 is not static.

And in the following you are trying to refer to Stack1 -

public static void main(String[] args) {
    Stack1 = new Stack(5);
}

from within main which is static.

Thus -

Cannot make a static reference to the non-static field Stack1

Your constructor name is Stack which does not match class name is stack. So -

The constructor Stack(int) is undefined

May be, you want to name your class Stack and in your main the following -

public static void main(String[] args) {
    Stack stack1 = new Stack(5);
}
沙沙粒小 2024-12-15 04:34:47

您的类被声明为小写的stack

Your class is declared as stack lowercase.

桃扇骨 2024-12-15 04:34:47

保留小写的类声明,我猜分配

   Stack1 = new Stack(5);

不是你的意思。相当:

Stack1 stack = new stack(5);

Keeping the lower case class declaration, I'm guessing the assignment

   Stack1 = new Stack(5);

is not what you meant. Rather:

Stack1 stack = new stack(5);
蘸点软妹酱 2024-12-15 04:34:47

Stack1stack 类的实例变量。在静态 main 方法中,您尝试将值存储在 Stack1 变量中,即使它是实例变量。另外,您尝试将非数组类型分配给类型为 Object数组的变量。此外,即使该类以小写形式称为 stack,您也有一个 Stack 的构造函数(注意大写)。

Stack1 is in instance variable to the stack class. In your static main method, you try to store a value in the Stack1 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 of Object. Furthermore, you have a constructor for Stack (note the uppercase) even though the class is called stack in lowercase.

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