Hello team, In this lesson we will learn what enumerations are and
how we can use them in Java. We are also going to
discuss how to declare enum and how we can compare them. I
will show you how enums can have properties and custom methods. But
first of all – what is Enum type? An enum type is
a special data type that enables for a variable to be a
set of predefined constants. The variable of enum type always must be
equal to one of the values that have been predefined for it.
When do we need enum types? We need enum types to express
some set of values that are not infinite. You need to use
enum for data sets where you know all possible values at compile
time. For example, seasons of the year – WINTER, SPRING, SUMMER, FALL.
There are very low chances that you will need to add additional
seasons in your program because no other exists. Or let’s review real
life scenario: together with students we implemented simple truth or dare game
for android. There are thousands of cards in the game. Each card
has its type and it can be either TRUTH or DARE. So,
in our program CardType will be enumeration. Does it make sense? To
make it clearer - let’s take a look at examples. To create
Enum make mouse right click on the package and select Enum. Here
you can specify Enum name and package. I’ve already created Enums to
save our time during the lesson. Here I have ‘Priority’ enum type.
In my program I’m going to have only three types of priority,
they are: high, medium, and low. I want to draw your attention
to the code style. Taking into account that all enum values
are constant we use capital letters according to java naming convention. Let’s
take a look how I use this enum. I can declare variable
of type Priority. As I mentioned before - the variable of enum
type always must be equal to one of the values that have
been predefined for it. You remember that one of the data types
that can be used in switch statement – is enum type. And
here is how you can use it. Based on the value of
‘priority’ variable during the runtime, different case block will be executed. In
this particular case when the value of our priority variable is HIGH
– this block will be executed. So now you know how to
declare custom enum type and how to create variable of enum type
to use it in the program. Let’s move on. One more option
to initialize enum variable. Let’s pretend you are dealing with user input
and you need to create enum based on the user input. You
can use ‘valueOf’ method to initialize enum variable. Just use enum type
name, call valueOf method and pass string to this method. In case
there are no enum value for the string you entered – IllegalArgumentException
will be thrown. You see when I pass ‘high’ in lowercase exception
is thrown. Take this into consideration while doing your homework. To compare
enumerations, you can use ‘equals to’ operator. And again, you remember that
Enumeration is not primitive type of data. Thus, you might think that
you should use ‘equals’ method to compare enums. Not necessarily. For example,
in this case we will get false as expected. And in this
case, we will get true as expected. Why? Because each enum variable
has its own ordinal. Take a look here one more time: you
have set of values which are known during the compilation. And they
are listed in the strict sequence. The HIGH has zero ordinal, the
MEDIUM - one and the LOW - two. Taking into account each
enum value has its own ordinal we can easily compare them via
‘equals to’ operator. You can use ‘ordinal()’ method to get ordinal of
each enum value. In some cases you might want to iterate over
all values from specific enum type. Just be aware that each enum
type has ‘values()’ method that returns array of enum values. You can
iterate over this array as you wish. You already know all
iteration statements in Java. And now probably the most interesting part. Each
enum value may have its own properties and methods. Let’s take a
look at this ‘Month’ enum type. We have next values: January, February,
March and so on. The list of constants is always terminated by
a semicolon. Definition of properties and methods always should go after the
list of constants. And here I declared a property – daysAmount. Each
of these months should have this property of int type. After that
I declare special method to create each enum. This special method is
called ‘constructor’. It is used to construct objects. Very soon we will
get to the object-oriented programming and I will explain you in detail
what constructor is. Also ignore this ‘private’ modifiers as of now, we
will discuss them during the OOP topic when we will talk about
encapsulation. Here you can see that I can pass int value to
constructor, and after I passed this value, I initialize ‘daysAmount’ property for
each enum. Here in parentheses I call constructor and pass int. Similar
to the method call, I call constructor and pass value to it.
So, for January, I pass thirty-one and daysAmount is initialized with thirty-one.
And you see ‘this’ keyword. This is new to you. That means
for THIS enum, in this specific case for JANUARY, the daysAmount will
be equal to thirty-one. And you see that local variable name is
also ‘daysAmount’. But ‘this.daysAmount’ and method argument ‘daysAmount’ are different variables. Because
‘this.daysAmount’ is this variable and ‘daysAmount’ is just this local variable.
For February I pass twenty-eight, and daysAmount is initialized
with twenty-eight value. For THIS enum, days amount will be twenty-eight. Then,
I can declare that each enum has its own behavior. Namely: each
month can tell me how many days it has. ‘this’ keyword means
THIS particular month. It can be January, February, March or any other.
Let’s take a look how we can use properties and methods of
Enum constants. We can call method getDaysAmount for January enum variable. And
for THIS month, for January, this is thirty-one. Hope it is clear
now. That’s it. Let’s take a look at what we have learned
from this lesson. From this lesson we learned what enum types are.
We can initialize enum variables, know how to compare them. Also we learned how
to iterate over all constants of specific enum type and learned that
enum type can have custom properties and methods. Now you know everything you need
to know to complete your homework. Let’s take a look at the
task. You need to implement simple console application with two enums. Try to implement
it by yourself and after that take a look at my solution.
And as always – you are welcome to ask questions in case you have
any. Thank you for your attention. See you in the next lesson.