Sling Academy
Home/Kotlin/What Are Enums in Kotlin?

What Are Enums in Kotlin?

Last updated: November 30, 2024

Understanding Enums in Kotlin

Enums in Kotlin are a powerful feature that provides a way to define a set of named constants. They are particularly useful when you need a predefined set of values that represent categories or states. In this article, we’ll explore how to define and use enums in Kotlin with various examples.

Defining a Basic Enum

In Kotlin, you can define an enum class by using the enum class syntax followed by the name of the enum, as shown below:

enum class Direction {
    NORTH, SOUTH, EAST, WEST
}

Here, Direction is an enum class with four different values: NORTH, SOUTH, EAST, and WEST.

Using Enums

To use an enum, you simply reference it by its type and name:

fun main() {
    val direction = Direction.NORTH
    println(direction)  // Output: NORTH
}

The above program initializes a variable with the enum constant NORTH and prints it.

Methods and Properties in Enums

Enums in Kotlin can have properties and methods. Here's how you can define a property and a method within an enum class:

enum class Color(val rgb: Int) {
    RED(0xFF0000),
    GREEN(0x00FF00),
    BLUE(0x0000FF);

    fun containsRed() = (this.rgb and 0xFF0000 != 0)
}

fun main() {
    println(Color.RED.containsRed()) // Output: true
    println(Color.GREEN.containsRed()) // Output: false
}

In this example, the Color enum has a property rgb and a method containsRed to check if the color contains the red component.

Enums with Annotations and Implementing Interfaces

Kotlin enums can implement interfaces, and each constant can provide its own method implementations. Here’s an example:

interface Printable {
    fun print()
}

enum class PrinterType : Printable {
    DOT_MATRIX {
        override fun print() {
            println("Printing with DOT_MATRIX")
        }
    },
    LASER {
        override fun print() {
            println("Printing with LASER")
        }
    },
    INKJET {
        override fun print() {
            println("Printing with INKJET")
        }
    }
}

fun main() {
    PrinterType.LASER.print() // Output: Printing with LASER
}

This example illustrates how each constant of PrinterType provides a specific implementation of the print method defined in the Printable interface.

Conclusion

Enums in Kotlin are a versatile tool that can greatly enhance your code's readability and safety by providing a predefined set of constant values. They are functional beyond just constants and can include properties, methods, and even multiple interface implementations.

Next Article: Defining and Using Enum Classes in Kotlin

Previous Article: Dynamic Dispatch in Kotlin: How Method Resolution Works

Series: Kotlin Object-Oriented Programming

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin