0
0
Kotlinprogramming~10 mins

Why enums constrain values in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why enums constrain values
Define enum with fixed values
Create variable of enum type
Assign only enum values allowed
Try to assign invalid value?
YesCompile error
No
Program continues
Enums define a fixed set of values. Variables of enum type can only hold these values. Trying to assign anything else causes an error.
Execution Sample
Kotlin
enum class Direction { NORTH, SOUTH, EAST, WEST }

fun main() {
    var dir: Direction = Direction.NORTH
    // dir = "UP" // Error: Type mismatch
    dir = Direction.EAST
    println(dir)
}
This code shows how a variable of enum type can only be assigned one of the enum's fixed values.
Execution Table
StepActionVariableValueResult
1Define enum Direction--Direction has values NORTH, SOUTH, EAST, WEST
2Declare variable dir of type DirectiondiruninitializedVariable created
3Assign dir = Direction.NORTHdirNORTHValid assignment
4Attempt dir = "UP" (commented out)dirNORTHCompile error if uncommented: type mismatch
5Assign dir = Direction.EASTdirEASTValid assignment
6Print dirdirEASTOutput: EAST
💡 Program ends after printing EAST; invalid assignments are caught at compile time.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
diruninitializedNORTHEASTEAST
Key Moments - 2 Insights
Why can't I assign a string like "UP" to the enum variable dir?
Because enums only allow their predefined values. The execution_table row 4 shows that assigning "UP" causes a compile error since it's not part of Direction.
What happens if I try to assign a value not in the enum?
The program won't compile. The error stops the program before running, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of dir after step 3?
Auninitialized
BEAST
CNORTH
D"UP"
💡 Hint
Check the 'Value' column in execution_table row 3.
At which step does the program detect an invalid assignment?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table row 4.
If you remove the comment on the invalid assignment, what happens?
ACompile error stops the program
BProgram prints "UP"
CProgram compiles and runs normally
Ddir variable becomes null
💡 Hint
Refer to the 'Result' in execution_table row 4 about compile errors.
Concept Snapshot
enum class Direction { NORTH, SOUTH, EAST, WEST }
- Enums define fixed allowed values.
- Variables of enum type accept only these values.
- Assigning other values causes compile errors.
- Helps prevent invalid data and bugs.
Full Transcript
Enums in Kotlin define a fixed set of named values. When you create a variable of an enum type, it can only hold one of those predefined values. For example, Direction enum has NORTH, SOUTH, EAST, and WEST. If you try to assign a string like "UP" to a Direction variable, the compiler will give an error and stop the program from running. This constraint helps catch mistakes early and keeps your program safe from invalid values. The execution steps show how the variable dir changes from uninitialized to NORTH, then EAST, and how invalid assignments are rejected.