0
0
Kotlinprogramming~15 mins

Why enums constrain values in Kotlin - See It in Action

Choose your learning style9 modes available
Why enums constrain values
📖 Scenario: Imagine you are building a simple app that tracks the status of orders in a store. Each order can only have one of a few fixed statuses, like Pending, Shipped, or Delivered. You want to make sure the status can never be set to something invalid by mistake.
🎯 Goal: You will create an enum class in Kotlin to hold the allowed order statuses. Then you will use this enum to assign and print the status of an order, showing how enums help keep values safe and limited.
📋 What You'll Learn
Create an enum class called OrderStatus with values PENDING, SHIPPED, and DELIVERED
Create a variable called currentStatus and set it to OrderStatus.PENDING
Change currentStatus to OrderStatus.SHIPPED
Print the value of currentStatus to show the current order status
💡 Why This Matters
🌍 Real World
Enums are used in apps to keep data clean and predictable, like order statuses, user roles, or traffic light colors.
💼 Career
Understanding enums is important for writing safe, clear code in Kotlin, which is widely used for Android apps and backend services.
Progress0 / 4 steps
1
Create the enum class for order statuses
Create an enum class called OrderStatus with the values PENDING, SHIPPED, and DELIVERED.
Kotlin
Need a hint?

Use enum class OrderStatus { PENDING, SHIPPED, DELIVERED } to define the fixed set of statuses.

2
Create a variable for the current order status
Create a variable called currentStatus and set it to OrderStatus.PENDING.
Kotlin
Need a hint?

Use var currentStatus = OrderStatus.PENDING to start with the pending status.

3
Change the current status to shipped
Change the variable currentStatus to OrderStatus.SHIPPED.
Kotlin
Need a hint?

Assign OrderStatus.SHIPPED to currentStatus to update the status.

4
Print the current order status
Write a println statement to print the value of currentStatus.
Kotlin
Need a hint?

Use println(currentStatus) to show the current status on the screen.