0

Hi so I have a class that has mulitple layers of nested classes within it and I was wondering why it does not work. For some reason I cant create a new StackNode I was wondering why that might be.

public class MultiStack {
    Stack1[] arr;
    int lim,numplate,index;

    MultiStack(int limit, int total) {
        lim = limit;
        numplate = total;
        int ssize =0;
        if(total%lim==0) {
            ssize = total/lim;
        } else {
            ssize = (total/lim) +1;
        }
        arr = new Stack1[ssize];
    }

    class Stack1 {
        StackNode top;
        int size;

        class StackNode{
            StackNode next;
            int value;
        }
    }
    public void push(int value){
        if(arr[index].size < lim) {
            push1(index);
        }
    }
    public void push1(int index) {
        arr[index].size++;
        StackNode cur = new StackNode(); //here is my issue
        cur.next = arr[index].top;
        arr[index].top = cur;
    }
}
randyMoss1994
  • 105
  • 1
  • 3
  • 10

1 Answers1

2

Neither class Stack1 nor class StackNode are static.

Therefore you need an instance of the surrounding class to instantiate it.

When you try to instantiate class StackNode you are in an object of MultiStack:

public void push1(int index) {
    arr[index].size++;
    Stack1.StackNode cur = new Stack1().new StackNode(); // here is my issue
    cur.next = arr[index].top;
    arr[index].top = cur;
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51