m0_70480119 2022-06-25 12:28 采纳率: 100%
浏览 23
已结题

Java static main method报错

问题遇到的现象和发生背景

在main method中,用构造器创建new ArrayQueue时报错。
non-static variable this cannot be referenced from a static context

问题相关代码,请勿粘贴截图

import java.util.Scanner;

public class ArrayQueueDemo{

public class ArrayQueue{
     int max_size ;
     int front ;
     int rear ;
     int[] queue;

//  construstor for the Arrayqueue
    public ArrayQueue(int size){
        max_size = size;
        front = -1;
        rear = -1;
        queue = new int[size];
    }

// check if it is full
    public boolean isFull(){
        return rear == max_size - 1;
    }
// check if it is empty
    public boolean isEmpty(){
        return front == rear;
    }
// adding items to the queue
    public void addQueue(int num){
        if(isFull()){
            throw new RuntimeException("the queue is full, cannot add");
        }
        rear++;
        queue[rear] = num;
    }
// get items from the queue
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException();
        }
        front++;
        return queue[front];
    }

// show all the items in the queue
    public void showQueue(){
        if(isEmpty()){
         System.out.println("queue is empty");
        }

        for (int i = 0; i < queue.length; i++) {
         System.out.println("the items are"+ queue[i]);
        }
    }

    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException();
        }
        return queue[front];
    }
}

public static void main(String[] args) {
   ** ArrayQueue arrq = new ArrayQueue(5);**
    char key = ' '; //input from the user
    Scanner scanner = new Scanner(System.in);
    boolean loop = true;
    while(loop){
        System.out.println("s(show): show queue");
        System.out.println("e(exit): exit queue");
        System.out.println("a(add): add items");
        System.out.println("g(get): pop element");
        System.out.println("h(show): get first element");
        key = scanner.next().charAt(0);
        switch (key) {
            case 's':
                arrq.showQueue();
                break;

            case 'a':
                System.out.println("enter a number");
                int value = scanner.nextInt();
                arrq.addQueue(value);
                break;
            case 'g':
               try {
                   int res = arrq.getQueue();
                   System.out.println("the items got poped is"+ res);
               } catch (Exception e) {
                   //TODO: handle exception
                   System.out.println(e.getMessage());
               }
                break;
            case 'h':
            try {
                int res = arrq.headQueue();
                System.out.println("the head item is"+ res);
            } catch (Exception e) {
                //TODO: handle exception
                System.out.println(e.getMessage());
            }
               break;
            case 'e':
                scanner.close();
                loop =false;
                break;
            default:
                break;
        }
    } 
    System.out.println("End");
}

}

运行结果及报错内容

non-static variable this cannot be referenced from a static context
No enclosing instance of type ArrayQueueDemo is accessible. Must qualify the allocation with an enclosing instance of type ArrayQueueDemo (e.g. x.new A() where x is an instance of ArrayQueueDemo).

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 一头小山猪 一头小山猪(个人IP)官方账号 2022-06-25 12:42
    关注
    1. 一个文件中只能有一个public class,这个class保持和文件名相同,如果你想使用多个类,需要放到不同的文件中,或者只用class关键字声明。
    2. 如果将两个类放在同一个文件中,要注意格式,你现在有两个类:ArrayQueueDemo 和 ArrayQueue,现在的写法是一种嵌套结构,这两个类要是平级的,且只保留一个public关键字,参考第一点,或者干脆放在两个文件中。
    3. 现在看来你的最外层类结构好像没有什么特别的作用,可以去掉,不知道这是不是你的完整代码。
      有帮助请采纳,还有不懂的可以继续追问~
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月24日
  • 已采纳回答 8月16日
  • 创建了问题 6月25日

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置