In prior releases, the standard way to represent an enumerated type was the int Enum pattern: // int Enum Pattern - has severe problems! public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3;
This pattern has many problems, such as: Not typesafe - Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which makes no sense). No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types. Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined. Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.
[ March 04, 2005: Message edited by: Carol Enderlin ]
Originally posted by rathi ji: Is this the only use of enum ??
One cool thing about enum is that the "values" can be used in a switch case.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
posted
0
Is what the only use of enum? Do you mean as a list of named constants? That is the simplest use of enums.
The link that Lionel included discussed "The new enum declaration defines a full-fledged class (dubbed an enum type)." They can have constructors, methods, instance variables.
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
posted
0
can any body please tell me , what is the difference between this 2 SOP ....
In my opinion , first one is returning a String and calling toString() method internally ...
And second one is returing Season ...
please reply ...
Thanks a lot . [ March 04, 2005: Message edited by: rathi ji ]
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
posted
0
Not sure what you're asking but here's your code compiled:
Change from: System.out.println(valueOf("WINTER")); To: System.out.println(Season.valueOf("WINTER"));
Fixed code compile and run:
C:\_Work\java\src>java EnumA WINTER WINTER
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
posted
0
Thank you very much , Actually I forgot that ... But my question is still the same ..
Both are printing WINTER , then what is the difference ...
Thanks .
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
posted
0
Well, one you could easily type wrong and your compiler won't help you with it (if you had one ) so you would only use the String version if that's all you had and you wanted to get the constant (perhpas based on user selection on a web page?).
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
In prior releases, the standard way to represent an enumerated type was the int Enum pattern
In prior releases, the common, but very ugly way to represent an enumerated type, was using int or some equivalent form. The correct way was to implement the Type-safe Enumeration design pattern, which is all a 1.5 enum is in bytecode (loosely speaking). Unfortunately, this simple concept didn't propagate too far, and atrocities occurred.