Example     counter example

class counter

{

int value;           //current value of a counter

int mx;              //max value of this

static int number=0; //number of counters

counter(int initial) //constructor

  {

  this.value = initial;

  this.mx = value;

  number++;          //creating counter

  }

void up()

  {

  value++;

  if(value > mx) mx = value;

  }

void down()

  {

  value--;

  }

int max()

  {

  return mx;

  }

} //class counter


counter a,b,c;

//counters don't exist yet

a = new counter(0);

b = new counter(0);

c = b;              //caution: c and b same

 

Link to source file counter.java.


a.up();

b.up();

c.up();

a.max() ==> ?  1

b.max() ==> ?  2

 

To Next Slide To Previous Slide To Contents