https://kotlinlang.org logo
#getting-started
Title
# getting-started
n

Nishoobansal

07/04/2020, 2:47 PM
can someone explain me enum classes. I didn't get what are they and it's my first time using enum classes
n

nglauber

07/04/2020, 3:53 PM
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

Michael de Kaste

07/04/2020, 6:57 PM
and enums are much more readable than an int ofcourse
👍 2
r

rajesh

07/05/2020, 9:16 AM
Just to add, sealed class is powerful version of enum class.
👍 1
3 Views