"non-static variable this cannot be referenced from a static context"is Java being very unhelpful - because where is the "this" it's talking about?
Thx, but where should I define the variable static ?
public class MyStack {
public interface Stack {
public void push( Object element );
public Object pop()
throws StackEmptyException;
public int size();
public boolean isEmpty();
public Object top()
throws StackEmptyException;
}
public class StackEmptyException extends RuntimeException {
public StackEmptyException( String err ) {
super( err );
}
}
public static class ArrayStack implements MyStack.Stack {
public static final int CAPACITY = 1000;
private int capacity;
private Object S[];
private int top = -1;
public ArrayStack() {
this( CAPACITY );
System.out.println("ArrayStack constructor called.");
}
public ArrayStack( int cap ) {
capacity = cap;
S = new Object[ capacity ];
}
public int size() {
return ( top + 1 );
}
public boolean isEmpty() {
return( top < 0 );
}
public void push( Object obj ) throws StackFullException {
if( size() == capacity )
//throw new StackFullException( "Stack overflow" );
S[ ++top ] = obj;
}
public Object top() throws StackEmptyException {
if( isEmpty() ){}
//throw new StackEmptyException( "Stack is empty." );
return S[ top ];
}
public Object pop() throws StackEmptyException {
Object elem;
if( isEmpty() ){}
//throw new StackEmptyException( "Stack is Empty." );
elem = S[ top ];
//System.out.println("");
S[ top-- ] = null;
return elem;
}
}
public class StackFullException extends RuntimeException {
public StackFullException( String err ) {
super( err );
}
}
public static void main(String[] args) {
//MyStack m = new MyStack();
//ArrayStack stack = m.new ArrayStack();
ArrayStack stack = new ArrayStack();
}
}