Use enums for array indexes in Java

Applying one usefull pattern together with an anti-pattern often fails 😉

In your case using an array for not-really array-like data provides a problem when you want to replace int constants with enum values.

A clean(er) solution would be something like an EnumMap with the enum values as keys.

Alternatively you could use table[COL_NAME.ordinal()] if you absolutely must.

If some API forces you to pass around int values but you have control over the actual values (i.e. you could pass your own constants), then you could switch to using enum values in your code and convert to/from enum only at the places where your code interfaces with the API. The reverse operation of enumValue.ordinal() is EnumClass.values()[ordinal]).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)