KaiquanMah commited on
Commit
2884d31
·
verified ·
1 Parent(s): 5e6376c

public static final int VARIABLE = 2;

Browse files
Week 6: Methods of OO Programming/14B. The Last Piece, Part 1: The Color Class ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ First, let's write a class called Color,
2
+ which has no other members than 2 integer class variables:
3
+
4
+ BLACK (value 2)
5
+ WHITE (value 1)
6
+
7
+ These variables must be marked with the 'final' modifier.
8
+
9
+
10
+ ====================================================
11
+
12
+
13
+ import java.util.Random;
14
+ import java.lang.reflect.Field;
15
+
16
+ public class Test {
17
+ public static void main(String[] args) {
18
+ final Random r = new Random();
19
+
20
+ System.out.println("Testing the Color class...");
21
+
22
+ try {
23
+ Field white = Color.class.getDeclaredField("WHITE");
24
+ System.out.println("WHITE is final: " + (white.getModifiers() > 10));
25
+ System.out.println("WHITE, value: " + Color.WHITE);
26
+
27
+ Field black = Color.class.getDeclaredField("BLACK");
28
+ System.out.println("BLACK is final: " + (black.getModifiers() > 10));
29
+ System.out.println("BLACK, value: " + Color.BLACK);
30
+ } catch (NoSuchFieldException e) {
31
+ // TODO Auto-generated catch block
32
+ e.printStackTrace();
33
+ } catch (SecurityException e) {
34
+ // TODO Auto-generated catch block
35
+ e.printStackTrace();
36
+ }
37
+ }
38
+ }
39
+
40
+
41
+
42
+ //ADD
43
+ class Color {
44
+ // public - allow access from other class
45
+ // static - class-level constants
46
+ // final - cannot be changed
47
+ public static final int BLACK = 2;
48
+ public static final int WHITE = 1;
49
+ }
50
+
51
+
52
+
53
+ Testing the Color class...
54
+ WHITE is final: true
55
+ WHITE, value: 1
56
+
57
+ BLACK is final: true
58
+ BLACK, value: 2
59
+
60
+
61
+
62
+
63
+