Updated April 1, 2023
Introduction to Kotlin enum
The kotlin enum is one of the datatypes that can be held to set the data constants for each. Every constant is a separate object. The constants are separated with some delimiters like a comma or other operators. So the enum also acts as a separate object like similar to the class. The enum instances are called whenever their methods are needed in the code. The enum constants also act as the anonymous class with their corresponding methods as well as similar to the overriding base methods. The enum class defines with the members and its definition with the semicolon operator.
Syntax of Kotlin enum
Kotlin language has many default concepts like class, method, enum etc. Enum also acts as the class, variables and other defined method datatypes.
enum class name{
---some declaration codes and methods if requires—
}
fun main()
{
-----Condition loops like if, for,… to iterate the enum values ----
}
The above codes are the basic syntax for declaring the enum constants, and they can be used and iterated on the main method. We can also call the enum values using the enum name. The value and it can be stored as a separate variable reference.
How does an enum work in Kotlin?
The kotlin enum classes are also used with the static keyword and its functionalities for to hold the reference variable value. Enum also acts as the interface; it is not derived from the class, but we can utilized and call the values and stored them as the separate variable. Since java enums are more ever used it as in the kotlin enums so that enums are acted as the classes as such that it also used as the anonymous class while it implements their own functions and attributes which is used on the specific logics like overriding and overloading concepts. While we are using overriding, the class’s abstract functions will be implemented in the child classes.
The main thing is the each enum constants have a separate reference for to hold the values in the memory location. These constants are compared with the comparable interface with the natural and sorted order are defined in the enum classes. An enum class of the kotlin has been defined with a new type only, and these can have their own properties and functions for implementing the application. The properties can be of the default value, and however, it is not provided with other constants that should be defined with its own value of the property.
Examples of Kotlin enum
Given below are the examples of Kotlin enum:
Example #1
Code:
package one;
enum class Months(val years: Boolean = false){
January(true),
February,
March,
April,
May,
June,
July(true),
August,
September,
October,
November,
December(true);
companion object{
fun details(mnths: Months): Boolean {
return mnths.name.compareTo("July") == 0 || mnths.name.compareTo("January") == 0
}
}
}
class first
{
var stdid:Int = 12
var stdName:String="arunkumar"
var stdRollno:Int=1234
var stdAddress:String="Sathyamoorthy street, moorthy nagar, chengalpattu district, chennai"
fun stddets(){
println("Welcome To My Domain this is the first example regarding the kotlin enum concept")
println("${this.stdid}")
println("${this.stdName}")
println("${this.stdRollno}")
println("${this.stdAddress}")
println("We have entered the student details like student id, student name, student roll number, student Address etc. the information’s are encrypted and it is stored in the separate database in cloud applications")
}
companion object Test{
var x: Int = 4
var empid:Int = 3
var empName:String="sivaraman"
var empSalary:Int=76562
var empAddress:String="Flat 12C, Sathya Enclave, Sathyamoorthy Nagar, Muthamizh Theru, chengalpattu district, chennai-600010"
fun details(){
println("Your details are: $x")
x++
}
fun empdetails(){
println("Welcome To My Domain this is the first example regarding the kotlin enum concept")
println("${this.empid}")
println("${this.empName}")
println("${this.empSalary}")
println("${this.empAddress}")
println("We have entered the student details like employee id, employee name, employee salary, employee Address etc. the information’s are encrypted and it is stored in the separate database in cloud applications")
}
}
}
fun main(args:Array<String>){
for(vars in Months.values()) {
println("${vars.ordinal} = ${vars.name} the months equals ${vars.years}")
}
val details = Months.February;
println("Is details the month equals ${Months.details(details)}")
first.details()
first.Test.details()
first.Test.empdetails()
println("Thank you users for spending the time with the companion object application")
}
Output:
In the above example, we declared month details using the enum.
Example #2
Code:
package one;
enum class Second(var sec: String) {
demo("first method"){
override fun exam() {
println("first method of the application")
}
},
demo1("second method"){
override fun exam() {
println("second method of the application")
}
},
demo2("third method"){
override fun exam() {
println("demo2 method of the application")
}
};
abstract fun exam()
}
class example{
var x: String = "Welcome To My Domain"
get() = field
set(eg) {
field = eg
}
}
interface inf {
val test: Int
fun eg() : String
}
class ex:inf{
override val test:Int=132
override fun eg()="Please try again"
}
inline fun <reified T> sample(lst: List<Any>): Boolean {
lst.forEach {
if (it is T) {
System.out.println("Welcome To My Domain its the second example that related to the kotlin enum concepts")
return true
}
}
System.out.println("Thank you users kindly write the code that related to the kotlin enum")
return false
}
fun main(args: Array<String>) {
Second.demo.exam()
val u = example()
u.x = "Have a Nice Day users please try again"
println(u.x)
val vars = ex()
sample<String>(listOf("arun", 5, args))
val vt = mapOf<String, Int>("Employee ID" to 57832, "SNO" to 1243, "ProofID" to 154)
for ((k,v) in vt) {
println("Your input key is: $k and your input value is $v")
}
vt.forEach { (k, v) ->
println("Your input key is: $k and your input value is $v")
}
}
Output:
In the second example, we calculate employee details using class and enum interfaces.
Example #3
Code:
package one;
fun main(args: Array<String>) {
val tv: TV = TV("Onida",TVColor.White)
val tv2: TV = TV("Vu",TVColor.IronGrey)
println("The color of my "+tv.tvname+" is "+tv.color)
println("The color of my "+tv2.tvname+" is "+tv2.color)
println(tv.color.toString() + " value is "+tv.color.value)
println(tv2.color.toString() + " value is "+tv2.color.value)
}
data class TV(val tvname:String, val color: TVColor)
enum class TVColor(val value: Int) {
White(0xFFFFFF),
IronGrey(0x52595d),
}
Output:
In the final example, we can use the TV requirements on the enum like colors etc.
Conclusion
In the kotlin language, they used the different concepts to implement the mobile-based applications with user requirements. Like that, enum is one of the features, and it is defined and initialized the constants with some advanced features like defining enum constants using some anonymous classes and enums while it implements the interfaces.
Recommended Articles
This is a guide to Kotlin enum. Here we discuss the introduction, syntax, and working of enum in kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –