Enumerations in JAVA

kherman

Golden Member
Jul 21, 2002
1,511
0
0
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.
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
This is from the API spec:

...to print all elements of a vector v:

for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}

You might get a better response in the Programming forum though...
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
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.
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.
 

manly

Lifer
Jan 25, 2000
13,565
4,233
136
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).

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:
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");
}
Notice the private constructor, so the only Suit instances you can play with are the ones declared in the class.
 

kherman

Golden Member
Jul 21, 2002
1,511
0
0
Originally posted by: manly
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).

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:
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");
}
Notice the private constructor, so the only Suit instances you can play with are the ones declared in the class.


I've seen this before somewhere.
 

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.
Right on. Good book too.