PDA

View Full Version : [SOLVED] [Java] Concerning enumerated data type



TreeFinger
March 29th, 2008, 03:12 AM
I am creating a class that has an attribute that will be an enumerated data type.

The object containing this enumerated data type will be constructed from a different class. When being constructed the constructor will require this enumerated value as a parameter.

Where should I create this enumerated data type?

Ramses de Norre
March 29th, 2008, 01:00 PM
In a separate class, an enum. Example (http://pastebin.com/m774de16e).

alexpwalsh
March 29th, 2008, 01:18 PM
You dont have to make a separate class in order to declare and use Enums ... you can declare them as a static variable as well.

eg:
Class move has the following enum....

//directions used for movement
public static enum direction{N, NE, E, SE, S, SW, W, NW}

.... and then use them statically

eg...
controller.movePiece(Move.direction.N);

now how you decide to implement and use them is up to you.
Hope I help....

Ramses de Norre
March 29th, 2008, 02:22 PM
I think you should only do that if the enum is clearly a part of the object your class is representing, if it is an entity of itself (like the direction of my example) your code will be cleaner when the enum is a class of itself. This is, of course, a matter of style and preferences, you're free to use the implementation that suits you best.