In today’s tutorial, we learn about Enum classes in Kotlin, which are used for predefined constants. Using Enum classes, we can define a set of type-safe enums, and each enum represents an object. We can add multiple enums inside a single enum class separated by a comma. Enum is used to define a fixed set of constant values in Kotlin.
Enum Classes in Kotlin: A Beginner’s Guide with Examples
Define Enum classes in Kotlin:
We will use the enum keyword to declare an enum class in Koltin. To add multiple constants in the enum class each constant should be separated by a comma. You can also define properties and functions in the enum class.
Code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
enum class Subjects { // Constants ENGLISH, MATHS, SCIENCE, PHYSICS, CHEMISTRY } fun main() { val todaySubject = Subjects.ENGLISH println("Today We will learn : $todaySubject") // Print Loop through all enum constants for (subject in Subjects.values()) { println(subject) } } // Output // Today We will learn : ENGLISH // ENGLISH // MATHS // SCIENCE // PHYSICS // CHEMISTRY |
Code explanation:
We have created an Enum class Subjects and adding multiple subject as constants into the class. We can directly call the constant using ClassName + Dot operator + Constant name.
Code example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
enum class Subjects(val description: String) { // Constants with descriptions ENGLISH("Today is English day."), MATHS("Today is Maths day"), SCIENCE("Today is Science day"), PHYSICS("Today is Physics day"), CHEMISTRY("Today is Chemistry day"); // Function to retrieve the description for each subject fun getEnglishDescription() { println(description) } fun getMathsDescription() { println(description) } fun getScienceDescription() { println(description) } fun getPhysicsDescription() { println(description) } fun getChemistryDescription() { println(description) } } fun main() { val todaySubjectEnglish = Subjects.ENGLISH val todaySubjectPhysics = Subjects.PHYSICS println("Today We will learn: ${todaySubjectEnglish.name}") todaySubjectEnglish.getEnglishDescription() // Prints the description of ENGLISH println("Today We will learn: ${todaySubjectPhysics.name}") todaySubjectPhysics.getPhysicsDescription() // Prints the description of ENGLISH // Print all subjects with their descriptions for (subject in Subjects.values()) { println("${subject.name}: ${subject.description}") } } |
Code explanation: In the above example, we added a custom function for each Enum and printed a short description of each subject. We can use these functions using emus later on.
Conclusion:
Enums are useful when we know the values we are going to use in our program or if they are Fixed for the whole application. So it’s completely up to us what type of values we want to make as Enums. They could be Navigation tab names, Color constants, etc. So keep practicing and you will master them, Happy coding.