Lädt...


🔧 Kotlin Enum Class: Properties and Usage


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Enum classes are used for working with a set of constant values. These classes are ideal for data structures that can be grouped together and, thanks to polymorphism support, help make code more organized and readable. In Kotlin, enums are defined as enum class and function as regular classes.

General Features

  • Enum classes cannot inherit from other classes.
  • Enums can implement interfaces. If an interface has a body-less (abstract) function, it must be overridden either in the enum class or within each enum constant.
  • You can define functions within enum classes, and both abstract and open functions can be used:
    • If you define an abstract function, all enum constants must override it.
    • open functions can be optionally overridden.

Image description
In this example, each team is defined as an enum constant, and the goal function is overridden. If an abstract function is defined, all enum constants must implement it.

  1. Enum constants are defined as static final in the background, which allows access to them without creating an object.

Constructors and Functions

Enum classes can have a primary constructor, and its visibility modifier must be private. This prevents the creation of enum constants as objects from outside, but the constants can use these parameters.

  • Secondary constructors can be defined in enums, though they are rarely used. If there is a primary constructor, additional actions can be performed using the init block.
  • Enum classes can include a companion object.
  • Enum constants can behave like classes and have constructor parameters. However, properties cannot be directly defined within an enum constant. Properties must be defined at the class level and accessed by the constants.

Enums also provide various functions:

  • values: Returns all enum constants as an array.
  • ordinal: Gives the order of the enum constant (0, 1, 2...).
  • name: Returns the name of the enum constant.
  • valueOf: Returns an enum constant by its name.

Image description
Enum constants behave like instances of a class. Each constant is an instance of the enum class.

For example:

enum class Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY
}

Here, MONDAY, TUESDAY, and WEDNESDAY are predefined instances of the Day class. There is no need to create a new object to access these constants:

val today = Day.MONDAY

This points to the MONDAY instance of the Day class. The Day.MONDAY expression acts as an instance of the class.

Why Use Enums?

  • Grouped Data: Enums are used to group constants of the same type. For example, to define user roles:
  enum class UserType { STUDENT, TEACHER, MANAGER, PARENT }
  • Comprehensive Control: When used in a when expression, enums ensure comprehensive control of constant values, preventing potential errors:
  when(userType) {     
    UserType.STUDENT -> println("Student")     
    UserType.TEACHER -> println("Teacher")     
    UserType.MANAGER -> println("Manager")     
    UserType.PARENT -> println("Parent") 
  }
  • Improved Code Organization: Since constants behave like classes, related code can be organized within enum constants, making the code more readable.

Underlying Structure of Enums

Enum constants are defined as static final classes in the background, which is why they are written in uppercase (e.g., GALATASARAY, FENERBAHCE).

Image description

Conclusion

Enum classes help make code more organized and readable when working with a set of constant values. They are ideal for managing and organizing groups of similar data. The fact that enum constants are defined as static final classes in the background allows access to them without creating new objects. Moreover, the support for polymorphism increases the flexibility and reusability of the code.

...

🔧 Kotlin Enum Class: Properties and Usage


📈 67.79 Punkte
🔧 Programmierung

🔧 Go Enum’s problems and solutions with xybor-x/enum


📈 41.21 Punkte
🔧 Programmierung

🔧 How to convert a TypeScript built-in enum to a GraphQL enum


📈 39.91 Punkte
🔧 Programmierung

🔧 Introducing TRLC Enum: A Modern C++ Enum Library for Enhanced Usability


📈 39.91 Punkte
🔧 Programmierung

🐧 How to Override the Properties of a CSS Class Using Another CSS Class


📈 32.6 Punkte
🐧 Linux Tipps

🔧 Dynamic Grid-Layout with Custom Properties and Resizable Elements (@properties)


📈 30.26 Punkte
🔧 Programmierung

🔧 Mastering CSS Custom Properties: The Senior Developer's Approach to CSS Custom Properties.


📈 28.96 Punkte
🔧 Programmierung

🔧 Kotlin Koans BR: Extension functions e properties (funções e propriedades estendidas)


📈 27.23 Punkte
🔧 Programmierung

📰 Programmiersprachen: Kotlin 1.2 RC ist freigegeben und Kotlin/Native unterstützt iOS


📈 25.49 Punkte
📰 IT Nachrichten

📰 Learn Kotlin Fast with new Kotlin Bootcamp course


📈 25.49 Punkte
🤖 Android Tipps

📰 Programmiersprachen: Kotlin 1.3.40 liefert Erweiterungen für Kotlin/JS und überarbeitet Typinferenz


📈 25.49 Punkte
📰 IT Nachrichten

📰 Kotlin 1.3.6 ist da: Update bringt Kotlin Worksheets


📈 25.49 Punkte
📰 IT Nachrichten

🎥 Kotlin: Using Room Kotlin APIs - MAD Skills


📈 25.49 Punkte
🎥 Video | Youtube

🎥 Kotlin: Using WorkManager Kotlin APIs - MAD Skills


📈 25.49 Punkte
🎥 Video | Youtube

📰 heise-Angebot: Rheinwerk Konferenz für Kotlin: Ein Tag voller Kotlin am 22. April


📈 25.49 Punkte
📰 IT Nachrichten

🔧 Kotlin Multiplataforma 101: Entendendo como o Kotlin compila para múltiplas plataformas


📈 25.49 Punkte
🔧 Programmierung

🔧 Decoding Kotlin - Your guide to solving the mysterious in Kotlin


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin Coroutines vs. Java Threads: A Concurrency Conundrum (Solved with a Sprinkle of Kotlin Magic!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin Type Inference vs. Java: A Deductive Dance (Where Kotlin Takes the Lead!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin Type Inference vs. Java: A Deductive Dance (Where Kotlin Takes the Lead!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin Operator Overloading vs. Java: A Mathematical Magic Show (Where Kotlin Bends the Rules!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin String Templates vs. Java String Concatenation: A Tale of Two Strings (Where Kotlin Sings!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Kotlin Lambdas with Receivers vs. Java: A Code Symphony (Where Kotlin Plays a Different Tune!)


📈 25.49 Punkte
🔧 Programmierung

🔧 Understanding Class Fields and Static Properties


📈 24.84 Punkte
🔧 Programmierung

🔧 Implement private properties in a JavaScript class


📈 23.54 Punkte
🔧 Programmierung

🔧 Data Class in Kotlin: The Easy and Efficient Way to Move Data


📈 23.1 Punkte
🔧 Programmierung

matomo