• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum - need help

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body explain me enum with some examples ...

thanks a lot .
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that clear enought ?...

Best regards,
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believed the Developer's notebook chapter 3 did give a good explanation on enum. Have you ever read that?

Nick
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



My doubt is how this two code is equivalent . I mean in above one WINTER is an integer & in the second one WINTER is Season & that is enum ...
Again ,


what it will print ???

please help me in this ...

Thanks a lot .
[ March 04, 2005: Message edited by: rathi ji ]
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, provided you fix your code to compile...(need comma between WINTER and SUMMER in your code snippet)...


C:\_Work\java\src>javac EnumPlay.java

C:\_Work\java\src>java EnumPlay
WINTER
[ March 04, 2005: Message edited by: Carol Enderlin ]
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's new - Enums:

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 ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this the only use of enum ??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic