SDSU CS 535: Object-Oriented Programming & Design
Fall Semester, 1997
Doc 8, The Static Issue

To Lecture Notes Index
San Diego State University -- This page last updated 24-Sep-97


Doc 8, The Static Issue Slide # 1
I have warned people about the use of static. There still seems to be some confusion over this issue. I hope the following will remove some of the problems over this issue. Now look at the following stack class. I have declared the fields as static. What this means is that there will only be one copy of the field elements and only one copy of the field topOfStack in any program. However we can trick ourselves. I show how we can do that on the next slide.

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;
   }
}


Doc 8, The Static Issue Slide # 2
Here is how we can trick are selves that the class StackWithStaticData is ok. In the class test we create only one stack and use its push and pop methods. The output is correct. So what is the problem? The next slide illustrates the problem.

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() );
   }
}


Output
7
5
3
1

Doc 8, The Static Issue Slide # 3

In this test program I create two stack objects. One called odd the other called even. I add numbers to each stack object. However I only print out the elements of the stack odd. Notice that the output contains all the numbers I put in both stacks. This is the problem with declaring the fields static. There is only one copy of the array elements in the program. Each stack object uses the same array. So each time I add an element to any stack in a program the element is added to the same array. This is the danger when one uses static incorrectly.

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() ); } }

Output
8
5
4
3
2
1

Doc 8, The Static Issue Slide # 4
 
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() );
   }
}

Doc 8, The Static Issue Slide # 5
In the following class StackWithBadMainBadFix I use a wrong way to address the compile errors produced when compiling the class StackWithBadMain. By making pop and push static the compiler does not complain about the use of pop and push in main. This leads us in the wrong direction. Now the compiler complains about the use of the array elements and the use of topOfStack in the methods pop and push. Again the error message states that we cannot access a non-static in a static method. Once we have gone this far the next step seems easy. I show this on the next slide.

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() );
   }
}

Doc 8, The Static Issue Slide # 6
In the class StackWithBadMainWorstFix we have finally satisfied that pesky compiler. I have made everything static! The class now compiles without any errors. We can even run the program! And it produces correct output. But as we saw earlier we do not want to have our data static. The lesson here is that the misleading error messages led us astray. We have a working program. However the class is worthless, as we can have only one stack in a program. In the next slide I will show the path we should take to correct the problem with the class StackWithBadMain.

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() );
   }
}


Doc 8, The Static Issue Slide # 7
Here is the proper way to correct the problem with the class StackWithBadMain. The problem was that we use the methods push and pop without a reference to an object. In the main in this class I first create a stack object. Then I send the method pop to the object. Notice the only place I have used static is in main. If this doesn't resolve the issue of using static please see me.

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() );
   }
}


visitors since 24-Sep-97