Hi! Imagine that you've been given the following task: create a class that implements the days of the week.
At first glance, this seems rather straightforward. Your code would look something like this:
Everything seems to be fine, but there is one problem: you can pass any text to the constructor of the
Here's what you should notice:
Now that looks a lot simpler :) Internally, our
The schedule object's
In the
This is one of
Now our
Everything seems to be fine, but there is one problem: you can pass any text to the constructor of the
DayOfWeek
class. That means someone could create a day of the week named "Frog", "Cloud" or "azaza322". This is clearly not the behavior that we expect, since there are only 7 real days of the week, and each of them has a specific name.
Therefore, our task is to somehow limit the range of possible values for the DayOfWeek
class.
Before Java 1.5 came along, developers had to independently invent their own solutions to this problem, since the language didn't have a ready-made solution.
In those days, if programmers needed to limit the number of values, they did this:
Here's what you should notice:
- The constructor is private. If a constructor is marked with the
private
modifier, it cannot be used to create an object. And since the class has only one constructor, noDayOfWeek
objects can ever be created. - Of course, the class does have the required number of
public static
objects, which were correctly initialized (using the correct names of the days of the week).This allowed these objects to be used in other classes.Output:DayOfWeek{title = 'Sunday'}
Enum
.
Enum
is also a class. It is specially "fine-tuned" to solving problems like this, i.e. creating a certain limited range of values. Java's creators already had ready examples (for example, C already had enum
), so they were able to create the best variant.
So what is Enum
in Java?
Let's revisit our DayOfWeek
example:
Now that looks a lot simpler :) Internally, our
Enum
has 7 static constants. And that's something we can use it to implement a program.
For example, let's write a program that determines whether a student needs to go to school today. Our student will have a daily schedule, represented by the StudentSchedule
class:
The schedule object's
dayOfWeek
variable determines which day is today.
And here's our student class:
In the
wakeUp()
method, we use Enum
to determine what the student should do next.
We didn't even provide details about each field in DayOfWeek
, and we don't need to: it's obvious how the days of the week are supposed to work. If we use it in its current form, any developer would understand what is happening in our code.
Another example of the convenience of Enum
is that its constants can be used with the switch statement. For example, let's write a program for a strict diet, in which dishes are scheduled by day:
This is one of
Enum
's advantages over the old solution used before Java 1.5 — the old solution could not be used with switch
.
What else do you need to know about
Enum
? Enum
is a real class with all the possibilities that this entails.
For example, if the current implementation of the days of the week is inadequate, you can add variables, constructors, and methods to DayOfWeek
:
Now our
Enum
constants have a title
field, getter, and overridden toString
method.
Compared to regular classes, one serious limitation was placed on Enum
— it cannot be inherited.
Additionally, enumerations have characteristic methods:
values()
: returns an array of all the values in theEnum
:Output:[DayOfWeek{title = 'Sunday'}, DayOfWeek{title = 'Monday'}, DayOfWeek{title = 'Tuesday'}, DayOfWeek{title = 'Wednesday'}, DayOfWeek{title = 'Thursday'}, DayOfWeek{title = 'Friday'}, DayOfWeek{title = 'Saturday'}]ordinal()
: returns the ordinal number of the constant. The numbering starts from zero:Output:0valueOf()
: returns theEnum
object that corresponds to the passed name:Output:DayOfWeek{title = 'Sunday'}
Enum
fields. These are constants so they use all-caps rather than camelCase
.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.