aspose file tools*
The moose likes Java in General and the fly likes enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "enum" Watch "enum" New topic
Author

enum

Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Hi,

I'm using an enumerated type to contains a set of strings like this:




In my client code, when I want to get a String which belongs to this enumeration I need to use:



This seems a bit long-winded to me. What I really want to do is define an enumeration of Strings, so that I can refer to members of this enumeration in my client code using:



Any idea how I can achieve this?

Thanks in advance,
DM

[ March 19, 2007: Message edited by: Dan Murphy ]
[ March 19, 2007: Message edited by: Dan Murphy ]

SCJP, SCJD, SCWCD
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Is it really necessary to use the extra variable code there?

If you make the constant names ZERO, ONE, etc., then you can just refer to the constants as MyEnum.ZERO or MyEnum.ONE.
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
I'm not sure if this is what you meant, but if you're referring to the fact that the default implementation of Enum.toString() returns the name of the constant itself, then that doesn't really help me.

The example I've shown above is a simplified version of my actual enum type. In the real version it would not be appropriate to name give the constants the same name as the String contents.

If this is not what you meant, then I'd be very appreciative if you could further explain (perhaps with an example), what you are suggesting.

Thanks,
DM
[ March 19, 2007: Message edited by: Dan Murphy ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I'm not sure I understand exactly what you're trying to do.

However, you can't make the constants in the Enum be Strings since they inherit from java.lang.Enum.
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
What I'm trying to do is simply define a fixed-collection of Strings using an Enum. I know I could achieve something similar with:

Set<String> myStrings = new HashSet<String>()

or

Map<String> myStrings = new HashMap<String>()

But neither of these give me the type-safety of an Enum. Also, with an Enum clients can use the Enum constants to easily refer to a member of the enumeration, whereas with a Map I need to define a separate set of keys, and with a Set the entire collection must be iterated over in order to find one particular member.

But from what you said it sounds like this isn't possible
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What are you using the String returned by the getCode() method for? Would it be more appropriate to pass around the enum itself instead of a String it refers to?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I see a number of options: redefine the enum's toString method; don't use Strings in the first place; don't use enums. There are probably more - which one is the appropriate very much depends on what you want to do with that set of strings. So, what do you want to do with it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
I don't want to redefine toString(), because this method is supposed to be used to print out a human-readable description for debugging, etc. In other words, a program shouldn't be functionally dependent on toString().

I want to use this Enum to define the valid values of a paramters, which are either 'd' or 't'.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Could you give a description of how you want to use 'd' and 't'?

Also have you looked at name() and valueOf()?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum