Friday, 6 September 2024

Kotlin enum class and Sealed class

 enum class:- 

  • Enum constants can  have properties, methods etc
  • Each object of enum class  act as separate instance of the class and is separated by commas.
  • It increase the readability of  code by assigning pre-defined names to constants.
  • An instance of an enum class cannot be created using constructors.
class MyKotlin {
fun main() {
println(MyEnum.MONDAY)
for (i in MyEnum.entries) {
println(i)
}
}

enum class MyEnum(i: Int, j: Int) {
SUNDAY(1, 3),
MONDAY(2, 4),
TUESDAY(3, 5),
WEDNESDAY(4, 6),
THRUSDAY(5, 7),
FRIDAY(6, 8),
SATURDAY(7, 9)
}
}

No comments:

Post a Comment

how to change status bar color in jectpack compose.

 Step1: Go to MainActivity and do following: enableEdgeToEdge ( statusBarStyle = SystemBarStyle .light( Color . Green .h...