Java Enum Example

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Hi! In this article we will discuss what enum is. You can find more articles in our Java Tutorial for Beginners.

Quite often, we need to pick out of a limited number of variants.

Tea, Cappuccino or Espresso?

Winter, Spring, Summer, Fall?

When we are at a restaurant, we have to make a choice out of a menu. With the help of enums, we can create a sort of "menu" - a limited set of values, out of which a user can choose something.

Here's an example of an enum:

As you see, the user has to make a choice out of 4 options: winter, summer, spring, fall.

Please note, that:

  • Enum is a stand-alone structure.  Enum can be a separate file or a part of a class. As you can see below, even in IDE enum stands alone from classes, interfaces, etc.

Enum must have an access modifier. If an enum is not nested inside of a class, it has to be  public. If you try to make it private, an exception will be thrown - see below. There's a logic behind this, if your enum is private, you can't use it. Right?

But if an enum is nested inside of a class, it can be private.

  • It is a sort of tradition that all "options" of enum (in fact -  Objects) have to be capitalized. If you write them in a lowercase, enum will work properly. Nevertheles, we recommend you to stick to a common practice and write all Objects of enum in capital letters.
  • Each Object is separated from another one with a comma. But after the last Object we need to write a semicolon:

Ok, you've got the idea. Now, let's see how it works in practice.

Example 1

First of all, let's create the same enum as we did above, but with the name "Seasons":

Second, let's create the main() method as you see below:

If you run this code on your computer, you will get the following: 

Now, I propose to take a closer look at our code. As you see, to call an Object, we write Seasons.WINTER, i.e. at first, we write the name of enum, then a period and the name of the Object.

Ok, but what if we have a bunch of objects in enum? Then it's not convenient to do it manually. Therefore, let's automate this with the help of a for-each loop. 

Example 2

In this example with the help of a for-each loop, we improve the code written in Example 1. If you don't know how the for-each loop works, you can read our article "For-Each Loop in Java"

As you see, we applied:

  • the method called values(), which enum has. This method returns an array of values stored in an enum.
  • for-each loop. With the help of the for-each loop we display values of each array element.

Example 3

It's very convenient to use enum with the if operator and the switch operator. 

Suppose we have the same enum, as we used in Example 1, i.e. Enum Seasons. Let's run this code in the main():

If you run this code on your computer, you will see the following:

Comments:

  • As you see, we have created the Enum Object:

Seasons arg = Seasons.FALL;

  • Then we applied the switch operator, which printed out a message depending on the season. Please note that in this case we don't need to write the whole path (for example, Seasons.WINTER), we write the name of the  Object (for example, WINTER):

case WINTER:

What else should you know about enum?

Ok, perfect! You already know what enum is and how to work with it. Still, you should know that Enum also has:

  • constructors
  • methods
  • and many more interesting stuff 🙂

We'll write a separate article on this.

You can find more articles in our Java Tutorial for Beginners.

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr
Самоучители--узнать детальнее--