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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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
1
00:00:00,000 --> 00:00:03,000
Hello team, In this lesson we 
will learn what enumerations are and 

2
00:00:03,000 --> 00:00:06,000
how we can use them in 
Java. We are also going to 

3
00:00:06,000 --> 00:00:10,000
discuss how to declare enum and 
how we can compare them. I 

4
00:00:10,000 --> 00:00:13,000
will show you how enums can 
have properties and custom methods. But 

5
00:00:13,000 --> 00:00:17,000
first of all – what is 
Enum type? An enum type is 

6
00:00:17,000 --> 00:00:20,000
a special data type that enables 
for a variable to be a 

7
00:00:20,000 --> 00:00:24,000
set of predefined constants. The variable 
of enum type always must be 

8
00:00:24,000 --> 00:00:28,000
equal to one of the values 
that have been predefined for it. 

9
00:00:28,000 --> 00:00:32,000
When do we need enum types? 
We need enum types to express 

10
00:00:32,000 --> 00:00:36,000
some set of values that are 
not infinite. You need to use 

11
00:00:36,000 --> 00:00:40,000
enum for data sets where you 
know all possible values at compile 

12
00:00:40,000 --> 00:00:46,000
time. For example, seasons of the 
year – WINTER, SPRING, SUMMER, FALL. 

13
00:00:46,000 --> 00:00:48,000
There are very low chances that 
you will need to add additional 

14
00:00:48,000 --> 00:00:53,000
seasons in your program because no 
other exists. Or let’s review real 

15
00:00:53,000 --> 00:00:57,000
life scenario: together with students we 
implemented simple truth or dare game 

16
00:00:57,000 --> 00:01:01,000
for android. There are thousands of 
cards in the game. Each card 

17
00:01:01,000 --> 00:01:05,000
has its type and it can 
be either TRUTH or DARE. So, 

18
00:01:05,000 --> 00:01:11,000
in our program CardType will be 
enumeration. Does it make sense? To 

19
00:01:11,000 --> 00:01:15,000
make it clearer - let’s take 
a look at examples. To create 

20
00:01:15,000 --> 00:01:19,000
Enum make mouse right click on 
the package and select Enum. Here 

21
00:01:19,000 --> 00:01:24,000
you can specify Enum name and 
package. I’ve already created Enums to 

22
00:01:24,000 --> 00:01:29,000
save our time during the lesson. 
Here I have ‘Priority’ enum type. 

23
00:01:29,000 --> 00:01:33,000
In my program I’m going to 
have only three types of priority, 

24
00:01:33,000 --> 00:01:37,000
they are: high, medium, and low. 
I want to draw your attention 

25
00:01:37,000 --> 00:01:41,000
to the code style. Taking 
into account that all enum values 

26
00:01:41,000 --> 00:01:45,000
are constant we use capital letters 
according to java naming convention. Let’s 

27
00:01:45,000 --> 00:01:49,000
take a look how I use 
this enum. I can declare variable 

28
00:01:49,000 --> 00:01:54,000
of type Priority. As I mentioned 
before - the variable of enum 

29
00:01:54,000 --> 00:01:57,000
type always must be equal to 
one of the values that have 

30
00:01:57,000 --> 00:02:01,000
been predefined for it. You remember 
that one of the data types 

31
00:02:01,000 --> 00:02:04,000
that can be used in switch 
statement – is enum type. And 

32
00:02:04,000 --> 00:02:08,000
here is how you can use 
it. Based on the value of 

33
00:02:08,000 --> 00:02:13,000priority’ variable during the runtime, different 
case block will be executed. In 

34
00:02:13,000 --> 00:02:17,000
this particular case when the value 
of our priority variable is HIGH 

35
00:02:17,000 --> 00:02:20,000this block will be executed. 
So now you know how to 

36
00:02:20,000 --> 00:02:24,000
declare custom enum type and how 
to create variable of enum type 

37
00:02:24,000 --> 00:02:27,000
to use it in the program. 
Let’s move on. One more option 

38
00:02:27,000 --> 00:02:32,000
to initialize enum variable. Let’s pretend 
you are dealing with user input 

39
00:02:32,000 --> 00:02:35,000
and you need to create enum 
based on the user input. You 

40
00:02:35,000 --> 00:02:40,000
can use ‘valueOf’ method to initialize 
enum variable. Just use enum type 

41
00:02:40,000 --> 00:02:44,000
name, call valueOf method and pass 
string to this method. In case 

42
00:02:44,000 --> 00:02:48,000
there are no enum value for 
the string you entered – IllegalArgumentException 

43
00:02:48,000 --> 00:02:53,000
will be thrown. You see when 
I pass ‘high’ in lowercase exception 

44
00:02:53,000 --> 00:02:57,000
is thrown. Take this into consideration 
while doing your homework. To compare 

45
00:02:57,000 --> 00:03:02,000
enumerations, you can use ‘equals to’ 
operator. And again, you remember that 

46
00:03:02,000 --> 00:03:07,000
Enumeration is not primitive type of 
data. Thus, you might think that 

47
00:03:07,000 --> 00:03:12,000
you should use ‘equals’ method to 
compare enums. Not necessarily. For example, 

48
00:03:12,000 --> 00:03:15,000
in this case we will get 
false as expected. And in this 

49
00:03:15,000 --> 00:03:20,000
case, we will get true as 
expected. Why? Because each enum variable 

50
00:03:20,000 --> 00:03:23,000
has its own ordinal. Take a 
look here one more time: you 

51
00:03:23,000 --> 00:03:27,000
have set of values which are 
known during the compilation. And they 

52
00:03:27,000 --> 00:03:31,000
are listed in the strict sequence. 
The HIGH has zero ordinal, the 

53
00:03:31,000 --> 00:03:36,000
MEDIUM - one and the LOW 
- two. Taking into account each 

54
00:03:36,000 --> 00:03:40,000
enum value has its own ordinal 
we can easily compare them via 

55
00:03:40,000 --> 00:03:44,000equals to’ operator. You can use 
‘ordinal()’ method to get ordinal of 

56
00:03:44,000 --> 00:03:48,000
each enum value. In some cases 
you might want to iterate over 

57
00:03:48,000 --> 00:03:53,000
all values from specific enum type. 
Just be aware that each enum 

58
00:03:53,000 --> 00:03:58,000
type has ‘values()’ method that returns 
array of enum values. You can 

59
00:03:58,000 --> 00:04:02,000
iterate over this array as you 
wish. You already know all 

60
00:04:02,000 --> 00:04:07,000
iteration statements in Java. And now 
probably the most interesting part. Each 

61
00:04:07,000 --> 00:04:11,000
enum value may have its own 
properties and methods. Let’s take a 

62
00:04:11,000 --> 00:04:15,000
look at this ‘Month’ enum type. 
We have next values: January, February, 

63
00:04:15,000 --> 00:04:20,000
March and so on. The list 
of constants is always terminated by 

64
00:04:20,000 --> 00:04:24,000
a semicolon. Definition of properties and 
methods always should go after the 

65
00:04:24,000 --> 00:04:29,000
list of constants. And here I 
declared a property – daysAmount. Each 

66
00:04:29,000 --> 00:04:34,000
of these months should have this 
property of int type. After that 

67
00:04:34,000 --> 00:04:38,000
I declare special method to create 
each enum. This special method is 

68
00:04:38,000 --> 00:04:43,000
called ‘constructor’. It is used to 
construct objects. Very soon we will 

69
00:04:43,000 --> 00:04:47,000
get to the object-oriented programming and 
I will explain you in detail 

70
00:04:47,000 --> 00:04:51,000
what constructor is. Also ignore this 
‘private’ modifiers as of now, we 

71
00:04:51,000 --> 00:04:55,000
will discuss them during the OOP 
topic when we will talk about 

72
00:04:55,000 --> 00:04:59,000
encapsulation. Here you can see that 
I can pass int value to 

73
00:04:59,000 --> 00:05:05,000
constructor, and after I passed this 
value, I initialize ‘daysAmount’ property for 

74
00:05:05,000 --> 00:05:11,000
each enum. Here in parentheses I 
call constructor and pass int. Similar 

75
00:05:11,000 --> 00:05:15,000
to the method call, I call 
constructor and pass value to it. 

76
00:05:15,000 --> 00:05:20,000
So, for January, I pass thirty-one 
and daysAmount is initialized with thirty-one. 

77
00:05:20,000 --> 00:05:24,000
And you see ‘this’ keyword. This 
is new to you. That means 

78
00:05:24,000 --> 00:05:30,000
for THIS enum, in this specific 
case for JANUARY, the daysAmount will 

79
00:05:30,000 --> 00:05:34,000
be equal to thirty-one. And you 
see that local variable name is 

80
00:05:34,000 --> 00:05:42,000
also ‘daysAmount’. But ‘this.daysAmount’ and method 
argument ‘daysAmount’ are different variables. Because 

81
00:05:42,000 --> 00:05:50,000this.daysAmount’ is this variable and ‘daysAmount’ 
is just this local variable.  

82
00:05:50,000 --> 00:05:53,000
For February I 
pass twenty-eight, and daysAmount is initialized 

83
00:05:53,000 --> 00:05:59,000
with twenty-eight value. For THIS enum, 
days amount will be twenty-eight. Then, 

84
00:05:59,000 --> 00:06:03,000
I can declare that each enum 
has its own behavior. Namely: each 

85
00:06:03,000 --> 00:06:08,000
month can tell me how many 
days it has. ‘this’ keyword means 

86
00:06:08,000 --> 00:06:13,000
THIS particular month. It can be 
January, February, March or any other. 

87
00:06:13,000 --> 00:06:17,000
Let’s take a look how we 
can use properties and methods of 

88
00:06:17,000 --> 00:06:21,000
Enum constants. We can call method 
getDaysAmount for January enum variable. And 

89
00:06:21,000 --> 00:06:26,000
for THIS month, for January, this 
is thirty-one. Hope it is clear 

90
00:06:26,000 --> 00:06:30,000
now. That’s it. Let’s take a 
look at what we have learned 

91
00:06:30,000 --> 00:06:35,000
from this lesson. From this lesson 
we learned what enum types are. 

92
00:06:35,000 --> 00:06:40,000
We can initialize enum variables, know how 
to compare them. Also we learned how 

93
00:06:40,000 --> 00:06:44,000
to iterate over all constants of 
specific enum type and learned that 

94
00:06:44,000 --> 00:06:48,000
enum type can have custom properties and 
methods. Now you know everything you need 

95
00:06:48,000 --> 00:06:52,000
to know to complete your homework. 
Let’s take a look at the 

96
00:06:52,000 --> 00:06:57,000
task. You need to implement simple console 
application with two enums. Try to implement 

97
00:06:57,000 --> 00:07:01,000
it by yourself and after that 
take a look at my solution. 

98
00:07:01,000 --> 00:07:04,000
And as always – you are welcome 
to ask questions in case you have 

99
00:07:04,000 --> 00:07:08,000
any. Thank you for your attention. 
See you in the next lesson.