Junior — Middle
What functions are available for use with enums in your programming language?
sobes.tech AI
Answer from AI
In Java, enumerations (enum) are a special type that represents a fixed set of constants. Besides storing a set of named values, enums in Java have the following features:
- Methods and fields: Enums can define fields, constructors, and methods, allowing to store additional information and behavior for each element.
values()method: Returns an array of all enum elements in the order they are declared.valueOf(String name)method: Returns the enum element with the specified name.- Comparison: Enum elements can be compared using
==andcompareTo(), as they are singletons.
Example:
public enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday");
private String englishName;
Day(String englishName) {
this.englishName = englishName;
}
public String getEnglishName() {
return englishName;
}
}
// Usage:
Day today = Day.MONDAY;
System.out.println(today.getEnglishName()); // Outputs: Monday