You are correct. As AmigaMan pointed out, the Enumeration class is an iterator for vectors in Java, and in no way equates to the enum concept from C. I am not sure if there is a way to do enum's in Java that equates to the C version. Typically it is done through a series of const variables. If you were feeling sadistic you could probably write a class that might mimic the same effect, but it would be more pain than it would be worth. All the C version really does is create a series of const int's that equate to the values.Originally posted by: kherman
IN JAVA, what is the proper way to do Enumerations. The Enumeration class doesn't seem to be the "traditional" way of doing enums. how do you handle it?
What is hte proper OO way of doing it?
I nomrally just create a bunch of static const ints.
Language-based enums such as in C/C++/C# are more type safe than static final ints, but neither is object oriented. The Enumeration collection class in Java is entirely different, and also more or less deprecated (use Iterator instead).Originally posted by: kherman
IN JAVA, what is the proper way to do Enumerations. The Enumeration class doesn't seem to be the "traditional" way of doing enums. how do you handle it?
What is hte proper OO way of doing it?
I nomrally just create a bunch of static const ints.
Notice the private constructor, so the only Suit instances you can play with are the ones declared in the class.public class Suit {
private final String name;
private Suit(String name) { this.name = name; }
public String toString() { return name; }
public static final Suit CLUBS = new Suit("clubs");
public static final Suit DIAMONDS = new Suit("diamonds");
public static final Suit HEARTS = new Suit("hearts");
public static final Suit SPADES = new Suit("spades");
}
Originally posted by: manly
Language-based enums such as in C/C++/C# are more type safe than static final ints, but neither is object oriented. The Enumeration collection class in Java is entirely different, and also more or less deprecated (use Iterator instead).Originally posted by: kherman
IN JAVA, what is the proper way to do Enumerations. The Enumeration class doesn't seem to be the "traditional" way of doing enums. how do you handle it?
What is hte proper OO way of doing it?
I nomrally just create a bunch of static const ints.
Look up the Java typesafe enum pattern in Joshua Bloch's Effective Java for an improved way of doing it, with the main downside being slightly greater memory usage. I'll copy a basic example from the book:
Notice the private constructor, so the only Suit instances you can play with are the ones declared in the class.public class Suit {
private final String name;
private Suit(String name) { this.name = name; }
public String toString() { return name; }
public static final Suit CLUBS = new Suit("clubs");
public static final Suit DIAMONDS = new Suit("diamonds");
public static final Suit HEARTS = new Suit("hearts");
public static final Suit SPADES = new Suit("spades");
}
Right on. Good book too.Originally posted by: manly
Look up the Java typesafe enum pattern in Joshua Bloch's Effective Java for an improved way of doing it, with the main downside being slightly greater memory usage.
