Mike Gaab said:
I am not up on recent Java features but I did look at the enum type prior to
deciding to use named constants in the example above. Seemed to me like
using Java enums confused the issue as they don't appear to be implemented
as they are in many other languages. Not that implementing them differently
is a bad thing , just not implemented as one would expect. For instance, how
do I assign a value to each element?
Like you assign values to other objects:
---
enum EnumWithAValue {
FOO(2),
BAR(42),
BAZ(87);
public final int value;
EnumWithAValue(int value) {
this.value = value;
}
}
// .... FOO.value == 2
---
You can also assign more than one value to each instance.
What are the default values?
There are none. The elements of an enumeration have nothing to
distinguish themselves by except their object identity (well, they do
have a name and an ordinal, but that's really implementation details).
In the case above one could expect the values to be Sun=0, Mon=1 and
so on but at a quick glance they did not appear to have those
values.
Correct. Java enumerations define a type with a fixed number of
values. It does so using a class with a fixed number of instances.
C-like languages instead define a fixed number of integer constants.
There is no type, so you cannot declare a variable to only take
members of the enumeration as values. I.e., the Java way is type-safe,
the C one is not.
The Java enumeration implementation is taken from the way people were
already implementing type-safe enums. It was explained in, e.g.,
"Java Language Programming Guide" by Joshua Bloch (Item 21 IIRC).
How do I get at each elements value to possibly re-assign
its value?
If you really want to be able to change it, make a settable value on
each instance. Otherwise, just set value by the constructor.
It really is just a class and instances of it. What makes it an enum
is that there can only be a fixed number of instances. (A singleton
is really just a trivial enumeration)
/L