can someone explain me enum classes. I didn't get what are they and it's my first time using enum cl...
n
can someone explain me enum classes. I didn't get what are they and it's my first time using enum classes
n
they are basically to establish a limited number of options which a value can be assigned to. For instance:
Copy code
// 0 is printed book, 1 is ebook
val bookType: Int = 0
But using this approach, you can assign an invalid value to this variable (2 for example) Creating a
enum
you limit the options to the two (or more if you want) possible values.
Copy code
enum class BookType { PRINTED, EBOOK }
So now you can define a variable/constant as
Copy code
val bookType: BookType = PRINTED
and you can’t assign an invalid value to `bookType`…
πŸ’― 3
πŸ‘ 1
m
and enums are much more readable than an int ofcourse
πŸ‘ 2
r
Just to add, sealed class is powerful version of enum class.
πŸ‘ 1