Well, there is Enum in which 3 variables are the Moderator Inkvizitor and God. How do I output their values ​​to an array? And does enum any privileges, unlike class or shortcomings, because both have fields and you can write methods to them.

  • 2
    Of course, the most important thing is that an enum object cannot be created using new. Therefore, they can always be compared through ==. Well, work as with any other object. - pavel

1 answer 1

You can use the YourEnum.values() method to get an array of all possible enumeration options.

Enumeration is more convenient than a class when you are planning an unscalable number of object instances. For example, an enumeration storing the sex of a person Sex.Male and Sex.Female . Also enumerations can have methods, for example:

 enum Sex { MALE, FEMALE; public String prefix() { return this == MALE ? "mr." : "mrs."; } } 

or

 enum Sex { MALE { public String prefix() { return "mr."; } }, FEMALE { public String prefix() { return "mrs."; } }; public abstract String prefix(); }