Home   Courses   Blog   About

What is an enum in Kotlin?

We have written about enums in Java, for those who read that article this can be a comparison to implementation in Kotlin. For others, that want to learn what an enum is in Kotlin, this is a good start.

We think that the question “What is an enum?” is important in Kotlin as well or in any other language. Enum is a very useful type in Kotlin. But what is an enum? Enums are used to list (or enumerate) known values. It is like having a list of constants that are listed together under a common namespace.

For example a lot of times you will see it for the 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 the 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 Kotlin 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, don’t forget to include Kotlin support as well (check on the bottom of the new project wizard))
  2. In activity_main.xml add a TextView (usually it is already there, if not create it), name it dayMessage
  3. Add a new file (Kotlin File/Class)
    •  
    • Name it Day
    • Inside this file we will put the code for our enum from above
  4. Now we will add a new variable to our MainActivity.kt
  5. 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.
  6. Run the app. First leave the value for the day as is, then change it to Day.FRIDAY to see the change (you will have to run it once again with value for the friday).

 

Enums can give use even more details about 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. Let us change the code for the day a little bit. The code in MainActivity will work with this changes. We will add a name and a short name for our days. And we can do that with the code bellow:

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.longName} to get long name from enum. But you can add even more functionalities to your enums. One that I liked is converting int to enum and back. So let’s check that on a new example.

 

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 local code and data. Imagine, that you get values from a server, then you 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 companion object cand a function fromInt that will do that for us:

There is a hidden gem in enum called values, that returns a list of types for us, so that we can compare the values and return the proper one. (since this is Kotlin we are talking about, you will need a semicolon this time after all the enum values, to separate it from the companion object). The final class would look something like this:

And we would use our new conversion like this:

 

Conclusion

With this topic 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.