The other day I tried to do this, but it doesn't work:
enum MyEnum {ONE = 1, TWO = 2}
much to my surprise, it doesn't compile!!! How to se custom ordinals???
You can't. Ordinals are fixed by starting at 0 and working up. From the docs for ordinal()
:
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
You can't assign your own values. On the other hand, you can have your own values in the enum:
public enum Foo { ONE(1), TWO(2); private final int number; private Foo(int number) { this.number = number; } public int getNumber() { return number; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With