Source of StaticDemo.java


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