What is an enum in Java?

We got a question the other day, what is enum? Enum is a very useful type in Java. Enums are used to list (or enumerate) known values. It is like having a list of constants that are listed together under common name.

For example a lot of times you will see it for days of the week.

This way you just write “Day.” and Android Studio will offer you options for days of the week. But this is not the only good thing about enums. Enums are also a way of keeping developers in check. You could easily use numbers for days, let say from 1 to 7. But some might prefer 0 to 6 since they are using a zero based array to get names of the days. Using enums would make sure, that we are talking about the same day since in the code it would be Day.MONDAY (not a value for it, 0 or 1) This makes the code more readable as well.

Enums in Java are very flexible since they are a type of class we can make our enums talk for us. We will show you that on an example.

  1. Make a new project (take a look at our blog on how to create an Android Studio project)
  2. In activity_main.xml add a TextView (usually it is already there, if not create it), name it dayMessage
  3. Prepare the initial code for MainActivity, it should look something like this:
  4. Add a new file (Java Class)
    • Name it Day
    • For kind choose Enum
    • Inside this file we will put the code for our enum from above
  5. Now we will add a new variable to our MainActivity.java
  6. We will also add a function that will do all the work. Now we have an enum for the day, we will write an extra happy message for friday and just a normal message for every other day. For friday we will also change the color of the text.

    • Now that we have an enum, comparing values just makes sense, since Android Studio will autocomplete all the values for us. Also we immediately see what we want with the if statement. If the comparison would look like (day == 5) we could have more options, now we know it is Friday.

 

 

Enums can give use even more details about a value

We will use the same example and develop it further. Let say that just having a DAY value standardized trough code is not enough and we want a little bit more. Enums can do this for us.

Now we can use an enum to tell us more about the value. In our case if we have a variable private Day weekday = Day.Monday;

We could use an enum with powers and print out the name of the day.

You can see that we changed the message for other days and added day.getName() from enum.

 

How can we convert the enum to int and int to enum ?

A lot of times you will have an enum, that will represent a type in some kind of database. Let say we store the transport type for our users, employees, friends… we would get an id in a form of an integer from the database. But we learned, that having an enum is great and we should use it trough our code.

We would start with something like:

And this is all good if we only work with our code if we get values from server, we would normally get an integer. But to make this example more interesting, numbers will not be sequential, the vehicle id is 5, car is 6, train is 4, bus is 10, walking is 0.

To get ids we must extend our example like this

This example works fine for us sending the number to the server and we would use it like:

(person would be the object with the value of the variable transport of type TransportType)

But how do we convert it from an integer (from the value we get from the server). We need to add a static function that will do that for us:

There is a hidden gem in enum called value, that returns a list of types for us, so that we can compare the values and return the proper one.

And we would use our new conversion like this:


 

Conclusion

With this topics covered we think you have enough knowledge about enums for quite a while. You will find ways to tweak it, but remember, enums will keep your code cleaner and easier to read.