Source of StaticDemo.java


  1: 
  2: public class StaticDemo
  3: {
  4:     private static int numberOfInvocations = 0;
  5:     
  6:     public static void main(String[] args)
  7:     {
  8:         int i;
  9:         StaticDemo object1 = new StaticDemo( );
 10:         for (i = 1; i <= 10; i++)
 11:            object1.displayCountOfInvocations( );
 12:                    
 13:         StaticDemo object2 = new StaticDemo( );
 14:         for (i = 1; i <= 10; i++)
 15:            object2.justADemoMethod( );
 16:                    
 17:         System.out.println("Total number of invocations = " +
 18:                             getNumberOfInvocations( ));
 19:     }
 20:     
 21:     public void justADemoMethod( )
 22:     {
 23:         numberOfInvocations++;
 24:         //In a real example, more code would go here.
 25:     }
 26:     
 27:     public void displayCountOfInvocations( )
 28:     {
 29:         numberOfInvocations++;
 30:         System.out.println(numberOfInvocations);
 31:     }
 32:     
 33:     public static int getNumberOfInvocations( )
 34:     {
 35:         numberOfInvocations++;
 36:         return numberOfInvocations;
 37:     }
 38: }