public class Robot {
public Robot(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int dx, int dy) {
x += dx; y += dy;
instanceMoveCount++;
classMoveCount++;
}
// return the move count for each instance of Robot
public int getInstanceMoveCount() {
return instanceMoveCount;
}
// return the total move count for all instances of Robot
public static int getClassMoveCount() {
return classMoveCount;
}
public int x, y;
private int instanceMoveCount;
static private int classMoveCount;
public static void main(String[] args) {
Robot r1 = new Robot(0, 0);
Robot r2 = new Robot(0, 0);
r1.move(20, 10);
r1.move(10, 20);
r2.move(10, 10);
int count1 = r1.getInstanceMoveCount();
int count2 = r2.getInstanceMoveCount();
// The next three statements will get the same result.
int count3 = r1.getClassMoveCount();
int count4 = r2.getClassMoveCount();
int count5 = Robot.getClassMoveCount();
System.out.println("count1 = " + count1);
System.out.println("count2 = " + count2);
System.out.println("count3 = " + count3);
System.out.println("count4 = " + count4);
System.out.println("count5 = " + count5);
}
}