DEV Community

deniz
deniz

Posted on

Kotlin Enum Class: Properties and Usage

Enum classes are used for working with a set of constant values. These classes are ideal for data structures that can be grouped together and, thanks to polymorphism support, help make code more organized and readable. In Kotlin, enums are defined as enum class and function as regular classes.

General Features

  • Enum classes cannot inherit from other classes.
  • Enums can implement interfaces. If an interface has a body-less (abstract) function, it must be overridden either in the enum class or within each enum constant.
  • You can define functions within enum classes, and both abstract and open functions can be used:
    • If you define an abstract function, all enum constants must override it.
    • open functions can be optionally overridden.

Image description
In this example, each team is defined as an enum constant, and the goal function is overridden. If an abstract function is defined, all enum constants must implement it.

  1. Enum constants are defined as static final in the background, which allows access to them without creating an object.

Constructors and Functions

Enum classes can have a primary constructor, and its visibility modifier must be private. This prevents the creation of enum constants as objects from outside, but the constants can use these parameters.

  • Secondary constructors can be defined in enums, though they are rarely used. If there is a primary constructor, additional actions can be performed using the init block.
  • Enum classes can include a companion object.
  • Enum constants can behave like classes and have constructor parameters. However, properties cannot be directly defined within an enum constant. Properties must be defined at the class level and accessed by the constants.

Enums also provide various functions:

  • values: Returns all enum constants as an array.
  • ordinal: Gives the order of the enum constant (0, 1, 2...).
  • name: Returns the name of the enum constant.
  • valueOf: Returns an enum constant by its name.

Image description
Enum constants behave like instances of a class. Each constant is an instance of the enum class.

For example:

enum class Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY
}
Enter fullscreen mode Exit fullscreen mode

Here, MONDAY, TUESDAY, and WEDNESDAY are predefined instances of the Day class. There is no need to create a new object to access these constants:

val today = Day.MONDAY
Enter fullscreen mode Exit fullscreen mode

This points to the MONDAY instance of the Day class. The Day.MONDAY expression acts as an instance of the class.

Why Use Enums?

  • Grouped Data: Enums are used to group constants of the same type. For example, to define user roles:
  enum class UserType { STUDENT, TEACHER, MANAGER, PARENT }
Enter fullscreen mode Exit fullscreen mode
  • Comprehensive Control: When used in a when expression, enums ensure comprehensive control of constant values, preventing potential errors:
  when(userType) {     
    UserType.STUDENT -> println("Student")     
    UserType.TEACHER -> println("Teacher")     
    UserType.MANAGER -> println("Manager")     
    UserType.PARENT -> println("Parent") 
  }
Enter fullscreen mode Exit fullscreen mode
  • Improved Code Organization: Since constants behave like classes, related code can be organized within enum constants, making the code more readable.

Underlying Structure of Enums

Enum constants are defined as static final classes in the background, which is why they are written in uppercase (e.g., GALATASARAY, FENERBAHCE).

Image description

Conclusion

Enum classes help make code more organized and readable when working with a set of constant values. They are ideal for managing and organizing groups of similar data. The fact that enum constants are defined as static final classes in the background allows access to them without creating new objects. Moreover, the support for polymorphism increases the flexibility and reusability of the code.

Top comments (0)