advance-Java Enumeration in Java #26

Veröffentlicht am: 18 April 2022
auf dem Kanal: 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.


Auf dieser Seite können Sie das Online-Video advance-Java Enumeration in Java #26 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer King of Techniques 18 April 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 62 Mal angesehen und es wurde von 10 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!