So let's start wit standard java enum:
package com.examples.coolEnums;
|
Many developers therefore fall back to using constants, something like this:
package com.examples.coolEnums; |
this is in general one of the possibilities, but you loose the advantage of type safety, compiler can't ensure in build time that the value assigned to some int variable is present in constants.
Let's take a different approach and spice things a little.
The way to do this is to define a constructor for the enum:
package com.examples.coolEnums;
|
this way, every enumeration has it's index value. You can access it by the intValue() method.
But what about the transformation of int to enum? Let's add a static method to EnumExample class:
public static EnumExample valueOf(int intCode)
|
you can extend the enum any way you want in a similar fashion. You could add compare methods, utils and many others.
The benefit of this approach is also that you still have the type safety ensured!