Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : E words : enumerated types.
public class Vegetable { public static final int unknown = 0; public static final int beet = 1; public static final int broccoli = 2; public static final int carrot = 3; } // end class Vegetable public class JuiceBar { public void mixIn (int v) { switch ( v ) { case Vegetable.broccoli: /* ... */ break; case Vegetable.carrot: /* ... */ break; default: /* ... */ } } // end mixIn } // end class Juicebar
public class Vegetable { protected Vegetable() { // constructor has no fields to initialise } public static final Vegetable unknown = new Vegetable(); public static final Vegetable beet = new Vegetable(); public static final Vegetable broccoli = new Vegetable(); public static final Vegetable carrot = new Vegetable(); } // end class Vegetable public class JuiceBar { public void mixIn (Vegetable v) { if ( v == Vegetable.broccoli ) { /* ... */ } } // ... } // end class JuiceBar
// Scott Andrew Borton approach public class Vegetable implements Serializable { private final int _type; protected Vegetable (int type) { _type = type; } public static final Vegetable unknown = new Vegetable(0); public static final Vegetable beet = new Vegetable(1); public static final Vegetable broccoli = new Vegetable(2); public static final Vegetable carrot = new Vegetable(3); private static final Vegetable[] vals = { unknown, beet, broccoli, carrot }; private Object readResolve() throws ObjectStreamException { return vals[_type]; } } // end class Vegetable
JavaWorld Magazine did an article on various ways to kludge enumerations in Java. Philip Bishop did an article on a type-safe scheme for both C++ and Java. John D. Mitchel did an article on a scheme using the C preprocessor.
I wrote a proposal to properly build in two types of enumerations into the Java glossary. I doubt we will ever have decent enumerations. The theoreticians seem to think the coming genericity features of Java will be sufficient. Arrgh! They don't care how verbose the code is.
Perhaps what we could do in the interim is use a combined approach with even more bells and whistles including a set of static final ints you can use as case labels. You also need some external representations for use in an external database, often a single character or a short string. These representations are more immune to breaking your database when you add a new enum element. You use an amanuensis to stomp out the repetitive code with a cookie cutter. You enter svelte Pascal and out pops Divine Java.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 2461. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/enumeratedtypes.html | J:\mindprod\jgloss\enumeratedtypes.html | |||