File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum - need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum - need help" Watch "enum - need help" New topic
Author

enum - need help

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
can any body explain me enum with some examples ...

thanks a lot .
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Isn't that clear enought ?...

Best regards,


Lionel Badiou
CodeFutures Software
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I believed the Developer's notebook chapter 3 did give a good explanation on enum. Have you ever read that?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830



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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Oct 11, 2004
Posts: 3830


Is this the only use of enum ??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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: 1364
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: 3830


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: 1364
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: 3830
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: 1364
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

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.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum - need help