Kotlin Enum Classes

Enums are special classes which limit the possible values of an object for that class. The possible values defined for that class are final or unchangeable.

The easiest way to define an enum is

enum class Direction {

Here each enum constant is an object.

This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-enum-classes/ and reposted on Medium on 16th Dec 2020.

For using these values

var direction : Direction = Direction.EAST

Base class of all Enum Classes

There is a base class for all enum classes which is defined in kotlin package.

* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
package kotlin/**
* The common base class of all enum classes.
* See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/enum-classes.html) for more
* information on enum classes.
public abstract class Enum<E : Enum<E>>(name: String, ordinal: Int): Comparable<E> {
companion object {}
* Returns the name of this enum constant, exactly as declared in its enum declaration.
public final val name: String
* Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant
* is assigned an ordinal of zero).
public final val ordinal: Int
public override final fun compareTo(other: E): Int /**
* Throws an exception since enum constants cannot be cloned.
* This method prevents enum classes from inheriting from `Cloneable`.
protected final fun clone(): Any
public override final fun equals(other: Any?): Boolean
public override final fun hashCode(): Int
public override fun toString(): String
* Returns an array containing the constants of this enum type, in the order they're declared.
* This method may be used to iterate over the constants.
* @values
* Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
* @throws IllegalArgumentException if this enum type has no constant with the specified name
* @valueOf

Name and Ordinal

These two properties are available by default for each enum constant as these are added in the base class itself.

Name is the actual name by which the enum constant is defined whereas ordinal is the sequence of that particular enum constant in the enum class having the first one starting from 0

val direction = Direction.NORTH
// this will print

Please continue reading at https://agrawalsuneet.github.io/blogs/kotlin-enum-classes/

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why acquire AWS certifications

AWS and Google Cloud Platform Networking— Concepts and Services

The Gorilla Guide to Serverless on Kubernetes — Chapter 2: Serverless’ Big Benefits

How To Perform Mobile Web Browser Testing [2022]

GSoC’21 @OpenMRS- Week 4

Multichannel to Open Banking to Data Insights: It All Starts with APIs

Python Flask for beginners

The Problem with Annotation Processors

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal


More from Medium

Kotlin Collections & Operations

Null Safety in Kotlin

How to Use TriState Toggle in Jetpack Compose

How to write null safe code with Kotlin? — Part 2