• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

enum - need help

 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body explain me enum with some examples ...

thanks a lot .
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that clear enought ?...

Best regards,
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is this the only use of enum ??
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?).
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic