| 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,000 |
| ‘priority’ 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,000 |
| – this 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,000 |
| ‘equals 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,000 |
| ‘this.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. |
|
|
|
|