Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where we use enums

 
Siva Rsrk
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enums are mostly used where you want to allow only a limited set of options. Each option will be an enum constant.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the real answer lies in combination of Sebastian & Rob's answers.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Rsrk
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, if you can group them together it's a common practice to use enums...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Siva Rsrk",
Please check your private messages like I had suggested here
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic