File size: 4,826 Bytes
4142885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
Last week we saw a few examples of storing constant values as static variables in a class. 
In older versions of Java, it was common to use static variables in this way:

class Suit {
    public static final int SPADES = 1;
    public static final int HEARTS = 2;
    public static final int CLUBS = 3;
    public static final int DIAMONDS = 4;
}





Now, the playing card class could look something like this:

class PlayingCard {
    private int suit;
    private int number;
    
    public PlayingCard(int suit, int number) {
        this.suit = suit;
        this.number = number;
    }

    public int getSuit() {
        return suit;
    }

    public int getNumber() {
        return number;
    } 
}




...and a new playing card could be created like this:

public static void main(String[] args) {
   PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
}
 




===============================================================



However, the method has a clear shortcoming: 
the constant numerical values do not automatically associate with their meanings. 

Even if we agreed that the numerical value 1 corresponds to the suit of spades in a deck of cards, 
requesting the suit will nevertheless return an integer:

public static void main(String[] args) {
   PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
   System.out.println("Card's suit: " + sevenOfSpades.getSuit());
   System.out.println("Card's number: " + sevenOfSpades.getNumber());
}

The program prints:
Card's suit: 1
Card's number: 7
 

A better solution is to use the enum class introduced in Java 5.









==============================================




What is an enum class?

The purpose of an enum class is to define a SET of VALUES. 
Typical examples could be 
the suits in a card game, 
compass directions or 
the houses at Hogwarts.

The class is defined with the keyword 'enum'. 
In its simplest form, we just write the values that belong to the set 
inside the class definition, separated by commas:

enum Suit {
    SPADES, HEARTS, CLUBS, DIAMONDS
}
 





Now we can change the definition of the card game class so that the suit is of type 'Suit', not an integer. 
This also limits the possible values: the suit of the card cannot be anything other than one of the four defined in the enum class Suit.

class PlayingCard {
    private Suit suit;
    private int number;
    
    public PlayingCard(Suit suit, int number) {
        this.suit = suit;
        this.number = number;
    }

    public Suit getSuit() {
        return suit;
    }

    public int getNumber() {
        return number;
    } 
}
 












This is now seen not only when creating a card, but also when printing the card's suit:

public static void main(String[] args) {
   PlayingCard sevenOfClubs = new PlayingCard(Suit.CLUBS, 7);
   System.out.println("Card's suit: " + sevenOfClubs.getSuit());
   System.out.println("Card's number: " + sevenOfClubs.getNumber());
}

Now the program prints:
Card's suit: CLUBS
Card's number: 7









==============================================








Using Enum Values

Enum type values can be directly compared with Java's equality and inequality operators. 
// ==
// !=

If we want to check whether a card is a diamond, we can write:

public static void main(String[] args) {
    PlayingCard card = new PlayingCard(Suit.DIAMONDS, 9);
    if (card.getSuit() == Suit.DIAMONDS) {
        System.out.println("The card is a diamond!");
    }
}






==============================================



As another example, let's define another enum class, which includes subjects taught:

enum Subject {
    MATHEMATICS, PHYSICS, CHEMISTRY, BIOLOGY, SWEDISH
}




 

Now we can use the value as the type of the 'mainSubject' field in the 'Student' class, for example:

class Student extends Person {
    private int credits;
    private Subject mainSubject;

    public Student(String name, String email, int credits, Subject mainSubject) {
        super(name, email);
        this.credits = credits;
        this.mainSubject = mainSubject;
    }

    public int getCredits() {
        return credits;
    }
    
    public Subject getMainSubject() {
        return mainSubject;
    }
}






Now it's easy to write a method, for example, that picks out the mathematics students from the student list:

public static ArrayList<Student> mathematicians(ArrayList<Student> students) {
    ArrayList<Student> al = new ArrayList<>();
    for (Student student : students) {
        if (student.getMainSubject() == Subject.MATHEMATICS) {
            al.add(student);
        }
    }
    return al;
}
 

Enum classes also support more versatile (and complex) features, which are not covered in this course. 
You can find more information at
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html