CS 535: Object-Oriented Programming & Design |
---|
class StackWithStaticData { private static int[] elements; private static int topOfStack = -1; public StackWithStaticData( int stackSize ) { elements = new int[ stackSize ]; } public void push( int item ) { elements[ ++topOfStack ] = item; } public int pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } }
class Test { public static void main( String args[] ) { StackWithStaticData odd = new StackWithStaticData( 10); odd.push(1); odd.push(3); odd.push(5); odd.push(7); while ( !odd.isEmpty() ) System.out.println( odd.pop() ); } }
class Test { public static void main( String args[] ) { StackWithStaticData odd = new StackWithStaticData( 10); StackWithStaticData even = new StackWithStaticData( 10);odd.push(1); even.push(2); odd.push(3); even.push(4); odd.push(5); even.push(8); while ( !odd.isEmpty() ) System.out.println( odd.pop() ); } }
Now there are several ways in which one can be tricked into using static. I will illustrate one such dangerous path. In the following class there is an error in the main program. The error is trying to call the methods push and pop without using an object. However when you try to compile the program the error message is confusing. The error message states that you cannot reference a non-static from a static method. I will illustrate the wrong way to address this error message on the next slide.
class StackWithBadMain { private int[] elements; private int topOfStack = -1; public StackWithBadMain( int stackSize ) { elements = new int[ stackSize ]; } public void push( int item ) { elements[ ++topOfStack ] = item; } public int pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public static void main( String args[] ) { push( 3 ); System.out.println( pop() ); } }
class StackWithBadMainBadFix { private int[] elements; private int topOfStack = -1; public StackWithBadMainBadFix( int stackSize ) { elements = new int[ stackSize ]; } public static void push( int item ) { elements[ ++topOfStack ] = item; } public static int pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public static void main( String args[] ) { push( 3 ); System.out.println( pop() ); } }
class StackWithBadMainWorstFix { private static int[] elements; private static int topOfStack = -1; public StackWithBadMainWorstFix( int stackSize ) { elements = new int[ stackSize ]; } public static void push( int item ) { elements[ ++topOfStack ] = item; } public static int pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public static void main( String args[] ) { push( 3 ); System.out.println( pop() ); } }
class Stack { private int[] elements; private int topOfStack = -1; public Stack( int stackSize ) { elements = new int[ stackSize ]; } public void push( int item ) { elements[ ++topOfStack ] = item; } public int pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public static void main( String args[] ) { Stack odd = new Stack( 5); odd.push( 3 ); System.out.println( odd.pop() ); } }