advance-Java Enumeration in Java #26

Publié le: 18 avril 2022
sur la chaîne: King of Techniques
62
10

#java #coreJava #Enumeration #enums

‪@kingoftechniques6864‬

Enumerations serve the purpose of representing a group of named constants in a programming language. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. Other examples include natural enumerated types (like the planets, days of the week, colors, directions, etc.).

Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time.

In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).

Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.

Every enum constant represents an object of type enum.
Enum and Inheritance:

All enums implicitly extend java.lang.Enum class. As a class can only extend one parent in Java, so an enum cannot extend anything else.
toString() method is overridden in java.lang.Enum class, which returns enum constant name.
enum can implement many interfaces.
values(), ordinal() and valueOf() methods:

These methods are present inside java.lang.Enum.
values() method can be used to return all values present inside the enum.
Order is important in enums.By using the ordinal() method, each enum constant index can be found, just like an array index.
valueOf() method returns the enum constant of the specified string value if exists.


Sur cette page du site, vous pouvez voir la vidéo en ligne advance-Java Enumeration in Java #26 durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur King of Techniques 18 avril 2022, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 62 fois et il a aimé 10 téléspectateurs. Bon visionnage!