This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes where we use enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where we use enums" Watch "where we use enums" New topic
Author

where we use enums

Siva Rsrk
Greenhorn

Joined: Jun 15, 2009
Posts: 14
Hi Java Achivers,

Just i read enums in K & B book, after reading this i understand the use of enums , but i didn't understand properly where we use this and Is it really useful in real time situations.
Can any one help me for this.

Thanks & Regards
Siva
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
If you have constants that are not likely to change in the future, you would use an enum.

Imagine a card example. The suit of a card will always be either HEART, DIAMOND, SPADES or CLUB. This is a perfect scenario for using an enum.

The real power of enums can be leveraged when using it's constructor capability and generating methods on it.


JDBCSupport - An easy to use, light-weight JDBC framework -
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Enums are mostly used where you want to allow only a limited set of options. Each option will be an enum constant.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
i think the real answer lies in combination of Sebastian & Rob's answers.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

In real-world programs, you sometimes need a set of related constants. Suppose you for example have a system that manages documents, and each document has a status. An enum would be a natural choice for the set of possible status values.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Siva Rsrk
Greenhorn

Joined: Jun 15, 2009
Posts: 14
What i understand from Sebastian & Jesper, instead of declearing 4 final varibles(from Card Example), then we maintain these 4 final varibles in a single unit called enum, right.
And one more usage is to maintain like a key & value pairs.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Exactly, if you can group them together it's a common practice to use enums...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This page explains why creating a number of final static variables is not good (it's not type-safe), and how enums in Java work.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Depends on the static variable type. In fact, the following are more or less equivalent:
However, enums give you several extra benefits:
- they are Serializable while still ensuring the defined constants are the only instances
- they can be used in switch statements
- there are automatic methods for retrieving all values or a constant based on the name - no need to create those yourself
- ...

So yeah, enums rock


Sidenote: you can actually create equivalents to enums in Java 1.4 and before; in fact, I've done so for a back-ported project. However, there is one thing you will never ever get working, and that's the switch statement support.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
One thing that Rob's pseudo-enum can do that a real enum can't (short of changing the source code) is this:

Enums prevent other users from changing the values in your class, or from instantiating new objects of that class. This is why they are good for singletons.

John.
Abhishek Jadhav
Greenhorn

Joined: Jun 24, 2009
Posts: 3
Enums are mostly used in scenarios like If you want to restrict the user for selecting the values from other than the fixed set of values .

I think following example illustrates what i want to say:

Suppose there is requirement to restrict the user from selecting the values of coffee type other than HOT and COLD then you can create enum of CoffeeType with the values of HOT and COLD in this way user can't give any third value .

-Abhishek.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

"Siva Rsrk",
Please check your private messages like I had suggested here


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

John de Michele wrote:One thing that Rob's pseudo-enum can do that a real enum can't (short of changing the source code) is this:

Enums prevent other users from changing the values in your class, or from instantiating new objects of that class. This is why they are good for singletons.

No, you cannot do that with Rob's pseudo-enum, because the constructor in his pseudo-enum is private.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
No, you cannot do that with Rob's pseudo-enum, because the constructor in his pseudo-enum is private.


Looks like I didn't read carefully enough .

John.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
ENUMs can also be used with code within them. The following example shows how to create a simple importer for the Sale Class



I create the following ENUM



Notice how I include a new SomethingColumnImporter and a String. This creates an enum that knows how to import a string into the corresponding Sale bean and the setter to use to set the value. The addValue method uses reflexion to access the object row passed to it (in this case a Sale bean) and tries to convert the object field into the required type. For example in the case of SALEDATE, it will try to use its converter to convert field into a date and store it in the saleDate property by using the setSaleDate method.

I then create a GUI with combos that allow me to select an index from an Excel or CSV file. Each index is stored in an array 3 elements long (one for each field in the bean).



Then a simple for loop on the fieldKeys array will call the import on each column value.



Using enums helps maintain the import logic nicely organized and easily create arbitrary importer ENUMs. If I want to add another property to the Sale class I just need to edit the enum and declare a new one called SHIPDATE which knows how to import the new shipDate property:



It is also easy to handle things like internationalization. I can have enum SaleColumnEnumUS for US locale. Allowing me to handle month day year rather than day month year which I usually use ( I could just use USDateColumnImporter rather than the normal Date importer) . I can also manage things like pounds vs kilos.
Hope this helps to visualize a more real usage of enums.
 
Don't get me started about those stupid light bulbs.
 
subject: where we use enums
 
Similar Threads
alternative for interface
What is Enums?
Enums
Understanding Enum
Enums over constants