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).

我的解答思路和尝试过的方法
我想要达到的结果
  1. 一个文件中只能有一个public class,这个class保持和文件名相同,如果你想使用多个类,需要放到不同的文件中,或者只用class关键字声明。
  2. 如果将两个类放在同一个文件中,要注意格式,你现在有两个类:ArrayQueueDemo 和 ArrayQueue,现在的写法是一种嵌套结构,这两个类要是平级的,且只保留一个public关键字,参考第一点,或者干脆放在两个文件中。
  3. 现在看来你的最外层类结构好像没有什么特别的作用,可以去掉,不知道这是不是你的完整代码。
    有帮助请采纳,还有不懂的可以继续追问~

import java.util.Scanner;

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

    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 class ArrayQueueDemo {
    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");
    }
}