public class Robot
classMoveCount
1:
2: public class Robot {
3:
4: public Robot(int x, int y) {
5: this.x = x;
6: this.y = y;
7: }
8:
9: public void move(int dx, int dy) {
10: x += dx; y += dy;
11: instanceMoveCount++;
12: classMoveCount++;
13: }
14:
15: // return the move count for each instance of Robot
16: public int getInstanceMoveCount() {
17: return instanceMoveCount;
18: }
19:
20: // return the total move count for all instances of Robot
21: public static int getClassMoveCount() {
22: return classMoveCount;
23: }
24:
25: public int x, y;
26: private int instanceMoveCount;
27: static private int classMoveCount;
28:
29: public static void main(String[] args) {
30: Robot r1 = new Robot(0, 0);
31: Robot r2 = new Robot(0, 0);
32: r1.move(20, 10);
33: r1.move(10, 20);
34: r2.move(10, 10);
35: int count1 = r1.getInstanceMoveCount();
36: int count2 = r2.getInstanceMoveCount();
37:
38: // The next three statements will get the same result.
39: int count3 = r1.getClassMoveCount();
40: int count4 = r2.getClassMoveCount();
41: int count5 = Robot.getClassMoveCount();
42:
43: System.out.println("count1 = " + count1);
44: System.out.println("count2 = " + count2);
45: System.out.println("count3 = " + count3);
46: System.out.println("count4 = " + count4);
47: System.out.println("count5 = " + count5);
48: }
49:
50: }