wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Issue with assigning enum instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issue with assigning enum instance variable" Watch "Issue with assigning enum instance variable" New topic
Author

Issue with assigning enum instance variable

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I am using some info from a database and assigning it to the various instance variables of an object. Mostly the info from the database is a String, so I have very simple setter methods for these that take the String from the ResultSet and assign it to the relevant String instance variables of my object.

So far so good. What I've come unstuck on is that one of my object's instance variables is a reference to an enum, which I want to use as it's been suggested to me that it would make my code more readable. This can't be stored (I don't think) in a database, so I thought I could just store an int in the database and use this int as an argument to my object's setter method, which has code that uses that int as the index in the enum.

I've read up on enums and looked at the docs, and none of the methods allow you to say "I want the enum at index 2", which seems a bit odd to me as I would have thought lots of people would want to do that. Does anybody know if this is possible?

I think I could create an EnumMap with an int value as the key and the corresponding Enum as the value and use the EnumMap's get(Object key) method to return the enum, but this seems very long winded to me and I would be grateful for a second opinion.

Thanks in advance.

Joe


OCPJP 85%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Look in the Java™ Language Specification; it says there is a static method called values() which returns the enum members as an array.

Is that any use to you?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Also, since you DB is already using strings ... you can convert the enum to a string and back, with the name() and valueOf() methods respectively.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Cambell,

Thanks for your reply!

I took a look at the language specification. I won't pretend I understood too much, but based on what you said, I've implemented the following code which works:



Do you think this is the best way? Better than (or maybe similar to) an EnumMap?

Also I was wondering why the values() method was not in the docs.

Thanks for your help

Joe
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Thanks as well Henry.

I will explore those methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
There is a problem that the enum in the database and the enum in Java must have the same members, so EDB ⊂ EJava, otherwise there is a risk of a value coming from the database which doesn't correspond to the values in your enum.

The reason it isn't in the API is that the values() method is a static member of the type you write, not of the Enum<E> class. But the values method is mentioned in the Java™ Tutorials page. If you ever find the Java5 version of the Java Tutorials, look what it says about enums and enumerated types in other languages
 
wood burning stoves
 
subject: Issue with assigning enum instance variable