Yeah, I have started to do this too, so there are less potential errors.
I mean if you have Direction myDirection = NORTH, its safer than just using an int, because someone could just mix it up and do like myDirection = 7;
But some of my team hate enums, so I was just interested in the general consensus.
I don’t like enums in Java, because I think they missed the point. I just want a numerical constant, man. Java’s is basically just a class, it’s too overly complicated. I usually just use constant integers or strings.
public static final String TYPE_DOG = "TYPE_DOG";
public static final String TYPE_MUSKRAT = "TYPE_MUSKRAT";
In the Hotspot JVM (not the bytecode, so this is implementation dependent) it’s all turned into ints, and if you have more than 32 enum values, it’s (obviously) converted to a long. Beyond that, it uses byte[]. This is done to be able to transform the functionality of EnumSet to binary operators.
Dang. I feel for you, man. Having your ints shrink to 5 bits must be harsh… Even worse, your bytes are 1.25 bits each, and that’s before rounding errors!
…
Why can you only fit 32 different values in an int? Does only use powers of 2?
The whole point of enums is that they’re NOT numerical constants and that each set of enums is a distinct type from each other. This is why I can’t set my direction to COLOR_RED or my color to NORTH. And if you’re going to introduce distinct types in Java, you’re going to do it with classes, unless you’re keen on creating a parallel type system that only works for enums.
I use them mainly to eliminate invalid values when only a (very) limited range of values is expected. Sure, I agree that they are a little bit too powerful, but I’d rather have them too powerful than too weak, as long as that doesn’t come with a too big performance hit of course.
static public final int CENTER = 1 << 0;
static public final int TOP = 1 << 1;
static public final int BOTTOM = 1 << 2;
static public final int LEFT = 1 << 3;
static public final int RIGHT = 1 << 4;
Any elegant solutions to use an enum and still be able to do the equivalent of “CENTER | LEFT”?
Even before I started using dynamic and functional languages more heavily, I always felt there was a lot of cruft around enums. But being able to just do ‘blah’, ‘Blah’, or ‘:blah’ for denoting an atom/symbol, is so much easier then the whole ‘EnumClass::FOO_BAR’ + it’s declaration.
Another case i use enum is for avoid a maximum of boolean parameters. Why ? because they are too low significant when i read a call usage and i never remember what this boolean is for when reading code.
A small enum in theses cases make the code more readable and more extendable if in a long time the 2-values parameters (boolean) became a 2+ values…
Another case i use it is when i need to store values for a serie of predefined constants. In this case, i use an EnumMap that is really efficient.
I use enums all the time, and really like them - probably the best addition in Java 5 - not that that’s saying much!
I’m surprised by the number of people who seem to have this opinion on this thread. Too verbose? How? Using them at their simplest surely enums are less verbose, because you don’t have to assign them a value at creation or check for invalid values in use. And as for too powerful, it’s not like you have to use it or worry about it, but at times it’s useful. Eg. I sometimes use fields to map Java enums to native enums in a type safe way, such as here
Yes, flags = EnumSet.of(CENTER, LEFT) along with flags.contains(CENTER), etc.
The elegant bit is how this maps down underneath to (practically) identical code. And you get type-safety, both for individual flags, as well as a separation between a set of flags and the flags themselves.
Dang, I think I heard the whooosh as you missed the point! Bitsets means one bit per value - so yes, in powers of 2.
CENTER_LEFT. Or as explained above, a Set containing CENTER and LEFT, which there’s some library support for already.
The point of enums is that they fully enumerate the possible values, and that no new values of that type can be synthesized. If you need them to act like bits, you don’t really have an enum. Now for compatibility’s sake, such as with OpenGL’s many many constants, you can assign values to the enums, but for sake of your particular example, it wouldn’t gain you any benefit in terms of syntax.