I'm trying to implement a stack using Arrays in Java. My Stack class consists of non static methods push, pop, peek and isempty. I want to test the stack implementation be instantiating the stack in a non static main method within a main class. When I try to do that I get an error "non-static method push(int) cannot be referenced from a static context" What am I doing wrong ?
Stack.java
public class Stack {
private int top;
private int[] storage;
Stack(int capacity){
if (capacity <= 0){
throw new IllegalArgumentException(
"Stack's capacity must be positive");
}
storage = new int[capacity];
top = -1;
}
void push(int value){
if (top == storage.length)
throw new EmptyStackException();
top++;
storage[top] = value;
}
int peek(){
if (top == -1)
throw new EmptyStackException();
return storage[top];
}
int pop(){
if (top == -1)
throw new EmptyStackException();
return storage[top];
}
}
Main.java
public class Main {
public static void main(String[] args) {
new Stack(5);
Stack.push(5);
System.out.println(Stack.pop());
}
}