What if you could avoid bugs caused by simple typos in your fixed options forever?
Why Enum class declaration in Kotlin? - Purpose & Use Cases
Imagine you need to represent a fixed set of options, like days of the week or traffic light colors, by writing separate constants or strings everywhere in your code.
This manual way is slow and risky because you might mistype a value, forget to update all places, or mix up values, causing bugs that are hard to find.
Enum classes let you group related constants in one place with clear names, making your code safer, easier to read, and less error-prone.
val red = "RED" val green = "GREEN" val blue = "BLUE"
enum class Color {
RED, GREEN, BLUE
}With enum classes, you can handle fixed sets of values confidently and clearly, enabling safer and cleaner code.
Think of a traffic light system where you only want to allow the colors RED, YELLOW, and GREEN to control the lights without mistakes.
Manual constants can cause mistakes and confusion.
Enum classes group fixed options clearly and safely.
They make your code easier to maintain and understand.
Practice
enum class in Kotlin?Solution
Step 1: Understand enum class purpose
An enum class is designed to hold a fixed set of constants that belong together logically.Step 2: Compare options with enum usage
Only To group a fixed set of related constants under one type correctly describes grouping related constants under one type, which is the main use of enum classes.Final Answer:
To group a fixed set of related constants under one type -> Option BQuick Check:
Enum class purpose = group constants [OK]
- Thinking enums are for creating multiple instances
- Confusing enums with regular classes
- Believing enums can hold any data type variable
Direction with constants NORTH, SOUTH, EAST, and WEST?Solution
Step 1: Recall Kotlin enum class syntax
Kotlin enum classes start withenum classfollowed by the name and curly braces containing constants separated by commas.Step 2: Check each option's syntax
enum class Direction { NORTH, SOUTH, EAST, WEST } matches the correct syntax. Options A, C, and D have incorrect keywords, punctuation, or structure.Final Answer:
enum class Direction { NORTH, SOUTH, EAST, WEST } -> Option AQuick Check:
Correct enum syntax = enum class Direction { NORTH, SOUTH, EAST, WEST } [OK]
- Using 'class enum' instead of 'enum class'
- Separating constants with semicolons instead of commas
- Assigning enum constants with '=' instead of listing
enum class Color { RED, GREEN, BLUE }
fun main() {
val c = Color.GREEN
println(c.name)
}Solution
Step 1: Understand enum constant property 'name'
Each enum constant has a built-innameproperty that returns its exact name as a string.Step 2: Analyze the code output
The code printsc.namewherecisColor.GREEN, so it prints "GREEN" exactly as declared.Final Answer:
GREEN -> Option DQuick Check:
Enum constant name property = GREEN [OK]
- Expecting lowercase output
- Printing enum constant directly without .name
- Confusing ordinal number with name
enum class Size {
SMALL,
MEDIUM
LARGE
}Solution
Step 1: Check enum constants separation
Enum constants must be separated by commas except the last one. Here,MEDIUMis missing a comma after it.Step 2: Validate other options
Constants can be uppercase, no constructor is required, and no minimum count is needed. So only missing comma is the error.Final Answer:
Missing comma after MEDIUM -> Option CQuick Check:
Enum constants separated by commas [OK]
- Omitting commas between constants
- Using lowercase for constants (not required)
- Thinking constructor is mandatory
rgb to an enum class Color that stores the color's RGB integer value, and access it for Color.RED? Choose the correct code snippet.Solution
Step 1: Understand enum property declaration
To add a property, declare a constructor parameter in the enum class and pass values to each constant.Step 2: Check each option for correct syntax
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } correctly declaresval rgb: Intin constructor and assigns values. enum class Color { RED = 0xFF0000, GREEN = 0x00FF00, BLUE = 0x0000FF } fun main() { println(Color.RED.rgb) } uses invalid assignment syntax. enum class Color { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } misses constructor declaration. enum class Color { RED, GREEN, BLUE } fun main() { println(Color.RED.rgb) } has no property defined.Final Answer:
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } -> Option AQuick Check:
Enum property via constructor = enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } [OK]
- Trying to assign values with '=' inside enum constants
- Not declaring constructor parameters in enum class
- Accessing property that was never declared
Ready to prove it?
Pass the test and this pattern moves unknown
