At least with J2SE 1.5 onward, you can specify additional enumeration attributes. This means that you could replace the entire whole if-else line with something that looks like
var2 = var1.getNextInSequence();
Now, in this case, it looks like you want the attribute to be a link to another enumeration that adds some wrinkles, for example, you cannot forward reference enumerations when they are initialized, but a solution for you in this way may be workable.
If the attributes are not other instances of the same enumeration, this will work:
public enum Animal { FOX(4), CHICKEN(2), WORM(0); private int countLegs; Animal(int n) { countLegs = n; } public int getLegCount() { return countLegs; }
But when the enumeration is self-referential, you have to be careful with the order in which your instances are declared. Ie this will have some problems:
public enum Animal { FOX(4, CHICKEN),
So, if you need a circular set of links among the enumerations, you will have to work on something else, but if not, you can use this technique, although you may have to order enum instances just to make it work.
Justjeff
source share