[go: up one dir, main page]

0% found this document useful (0 votes)
7 views2 pages

Enum Class in Kotlin

Uploaded by

manojkdh30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Enum Class in Kotlin

Uploaded by

manojkdh30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Let’s break it down simply 👇

✅ What is Enum Class in Kotlin?


An enum class (short for enumeration class) is a special type of class in Kotlin used to
represent a fixed set of constants.​
It’s helpful when a variable can only take one value out of a limited set of predefined options.

📌 Syntax:
enum class Direction {

NORTH, SOUTH, EAST, WEST

Here, Direction is an enum class that has four constant values.

🎯 Uses of Enum Class:


1.​ Type-Safe Constants – Ensures only valid values are used (unlike plain strings or
integers).​

2.​ Improves Code Readability – Groups related constants together.​

3.​ Supports Properties & Methods – Each enum constant can have its own data or
behavior.​

4.​ Useful in Control Flow (when statements) – Easy to handle different cases.​

5.​ Prevents Invalid Inputs – You cannot pass values outside the enum set.​

📌 Example:
enum class Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY

fun main() {

val today = Day.MONDAY

when (today) {

Day.MONDAY -> println("Start of the week")

Day.FRIDAY -> println("Weekend is near!")

Day.SATURDAY, Day.SUNDAY -> println("It’s Weekend 🎉")


else -> println("Midweek Day")

👉 Output:
Start of the week

Would you like me to also create a short PDF file for this explanation (like the one you asked
earlier) so you can keep it as notes?

You might also like