
errorprone.bugpattern.EnumOrdinal.md Maven / Gradle / Ivy
The newest version!
You should almost never invoke the `Enum.ordinal()` method. The ordinal exists
only to support low-level utilities like `EnumSet`. The ordinal of a given enum
value is not guaranteed to be stable across builds because of the potential for
enum values to be added, removed, or reordered.
Prefer using enum value directly:
```java
ImmutableMap MAPPING =
ImmutableMap.builder()
.put(MyEnum.FOO, "Foo")
.put(MyEnum.BAR, "Bar")
.buildOrThrow();
```
to this:
```java
ImmutableMap MAPPING =
ImmutableMap.builder()
.put(MyEnum.FOO.ordinal(), "Foo")
.put(MyEnum.BAR.ordinal(), "Bar")
.buildOrThrow();
```
Or if you need a stable number for serialisation, consider defining an explicit
field on the enum instead:
```java
enum MyStableEnum {
FOO(1),
BAR(2),
;
private final int index;
MyStableEnum(int index) {
this.index = index;
}
}
```
© 2015 - 2025 Weber Informatics LLC | Privacy Policy