avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2339

Abstract

position in the enum class declaration.</p> <figure id="e792"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FuM9BSjJ2L&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FuM9BSjJ2L&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="340b">Kotlin also provides standard convenience functions for working with enums.</p> <figure id="272b"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fka061HYs0&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fka061HYs0&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="e716">Members can declare their own anonymous classes, as well as override base methods.</p> <figure id="ddba"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F1fDGK8OPk&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F1fDGK8OPk&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="2933">Enums can also implement interfaces. Each member can specify its own implementation, or a common implementation can be provided for all members (which can then be overridden if desired).</p> <f

Options

igure id="8de4"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FbXle3qOee&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FbXle3qOee&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="c2e7">You can access the constants in an enum class in a generic way using the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/enum-values.html"><code>enumValues<t< code="">>()</t<></code></a><code> and <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/enum-value-of.html"><code>enumValueOf<t< code="">>()</t<></code></a><code> functions:</code></code></p> <figure id="8f34"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FTsGNDY5WU&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FTsGNDY5WU&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="e721">Go back to <a href="https://readmedium.com/reified-type-parameters-dd70e7cc4ee1">Reified Type Parameters</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/infix-functions-f2c328941939">Infix Functions</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Enums

A quick tour of enums in Kotlin, the utilities at your disposal, how to have enums declare their own anonymous classes and implement interfaces, and how to work with enums in a generic way

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI++

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to extend express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Basic enums remain the same:

Every enum constant has properties for obtaining its name and position in the enum class declaration.

Kotlin also provides standard convenience functions for working with enums.

Members can declare their own anonymous classes, as well as override base methods.

Enums can also implement interfaces. Each member can specify its own implementation, or a common implementation can be provided for all members (which can then be overridden if desired).

You can access the constants in an enum class in a generic way using the enumValues>() and enumValueOf>() functions:

Go back to Reified Type Parameters, jump to the Table of Contents, or continue to Infix Functions.

Join me in Etnetera
Kotlin
Java
Programming
Enum
Object Oriented
Recommended from ReadMedium