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.
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.
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.
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.
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 .
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop