Java Enum Methods
All Enum Methods
An enum is a special type that holds a fixed group of constants (unchangeable values), for example enum Level { LOW, MEDIUM, HIGH }. Every enum that you create gets the methods below automatically.
The Java compiler (the tool that turns your code into a program) adds the static methods values() and valueOf() to every enum, so you call them on the enum name, for example Level.values(). The other methods come from the Enum class, and you call them on a constant, for example Level.HIGH.name(). In the table, E means the type of your enum, like Level.
| Method | Description | Return Type |
|---|---|---|
| compareTo() | Compares the position of this constant with another constant of the same enum | int |
| name() | Returns the name of the constant, exactly as it is written in the enum | String |
| ordinal() | Returns the position of the constant in the enum, starting at 0 | int |
| toString() | Returns the name of the constant, or your own text if the method is overridden | String |
| valueOf() | Returns the constant that has the given name | E |
| values() | Returns an array with all the constants of the enum, in the order they are written | E[] |
Related Pages
Java Enum Constructor Tutorial