aspose file tools*
The moose likes Java in General and the fly likes Enumerations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enumerations" Watch "Enumerations" New topic
Author

Enumerations

Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Although we have discussed many of the new features in Java 2, v5, there is one that has gone by practically without comment: enumerations. This might be because most programmers don't yet understand how powerful they are in Java. Here is how I start the chapter on enumerations in my book.


Since its original version, there has been one feature that many programmers felt was missing from Java: enumerations. In its simplist form, an enumeration is a list of named constants. Although Java offered other features, such as final variables that can provide somewhat similar functionality, many programmers still missed the conceptual purity of enumerations -- especially because enumerations are supported by many other commonly used languages. With the release of Java 2, v5, enumerations are now available to the Java programmer.

In their simplist form, Java enumerations appear similar to enumerations in other languages. However, this similarity is only skin deep. In languages such as C++, enumerations are simply lists of named integer constatns. In Java, an enumeration defines a class type. By making enumerations into classes, the concept of the enumeration is greatly expanded. For example, in Java, an enumeration can have constructors, methods, and instance variables. Therefore, although enumerations were several years in the making, Java's implementation has made them well worth the wait.


Anyone have any thoughts and comments about enumerations?

BTW: Enumerations also cause the new keyword enum to be added to Java, which is used to create one. Thus, they represent a rather high-profile addition.
[ August 25, 2004: Message edited by: Herb Schildt ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
enum is one of my favorite new additions, alongside generics and the enhanced for loop.

At the moment, I'm drawing a blank as to why Bloch et al. decided to not allow subclassing an enum. Anybody remember why this is? (Apparently I'm feeling too lazy to search the internet myself.)


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Consider if enums could extend other enums:

What output should we expect here? Some people might expect CrazySuits.values() to list all the inherited values too = except it can't, since those aren't CrazySuits, they're just Suits. And Suits.values() can't list all existing Suits instances, because it can't know about CrazySuites (which maybe didn't even exist when Suites was created and put in a jar or something). It might be possible to let the JVM look up all available extending classes dynamically, but that would probably lead to other errors. E.g. the code that allows enums to be used in switch statements probably depends on the compiler being able to know just what the possible values of the enum are, at compile time. There may be possible ways to resolve these issues, but I think it's the sort of thing that probably creates more confusion than benefit.


"I'm not back." - Bill Harding, Twister
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
So what you're saying is, we shouldn't be able to subclass enums ... but it would be okay to superclass them?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm saying it would be bad either way. One enum would be a subclass of the other other. Wacky highjinks would ensue.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
I used to use the enumeration feature of C++ and now we got it in Java!!!

Is Enumeration chapter one of the big chapter like generics in your book, Mr.Herbert, since it's a very brand new feature in J2SE 5.0?


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Is Enumeration chapter one of the big chapter like generics in your book, Mr.Herbert, since it's a very brand new feature in J2SE 5.0?


Mr.Herb's book covers new featues in Java Tiger, so every chapter must be abig one.



Groovy
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Pradeep Bhat:


Mr.Herb's book covers new featues in Java Tiger, so every chapter must be abig one.



but Mr.Herbert mentioned in a thread out there that Generics chapter is the biggest one... So I'm wondering whether the Enumeration chapter is as big as Generics chapter or not...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dirk Schreckmann:
enum is one of my favorite new additions, alongside generics and the enhanced for loop.


I fully agree with Dirk! We are already using the Typesafe Enum pattern very much - having language support will be very welcome!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Ilja Preuss:

We are already using the Typesafe Enum pattern very much - having language support will be very welcome!


Hi Ilja Preuss,
It seems that I am left behind... Are you using the brand new features of Tiger in your work already? What a fast your team is following the new technology!

Thanks for the information..
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ko Ko Naing:


Hi Ilja Preuss,
It seems that I am left behind... Are you using the brand new features of Tiger in your work already? What a fast your team is following the new technology!

Thanks for the information..


The Typesafe Enum pattern that Ilja refers to is not the same as the enumerations that are being introduced with Tiger. Typesafe Enum is the workaround that Java programmers have used for years to get similar behavior. Feel free to google for "java typesafe enum pattern" to find examples and a description of how it works.


Java API Documentation
The Java Tutorial
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Ko Ko:

The enumeration chapter is medium sized. Because it is so cleanly implemented, enum doesn't require as many pages to describe as one might first think. The longest two chapters are Generics (about 50 pages) and Formatted I/O (about 34 pages). The formatted I/O chapter is long because of the richness of the features that Formatter and Scanner offer.
[ August 26, 2004: Message edited by: Herb Schildt ]
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Layne Lund:


The Typesafe Enum pattern that Ilja refers to is not the same as the enumerations that are being introduced with Tiger. Typesafe Enum is the workaround that Java programmers have used for years to get similar behavior. Feel free to google for "java typesafe enum pattern" to find examples and a description of how it works.


I've ever used that feature before... but I've never known that that kind of usage is called typesafe enum pattern....

But it's good to know that it is called so... As you suggested, I googled for the topics about them and I found a cool article which is from JavaCamp.org... It explains about the typesafe enum pattern we are talking about... FYI, someone might wanna have a look at the Cool TypeSafe Enum Article I am talking about.....
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Herb Schildt:
Ko Ko:

The enumeration chapter is medium sized. Because it is so cleanly implemented, enum doesn't require as many pages to describe as one might first think. The longest two chapters are Generics (about 50 pages) and Formatted I/O (about 34 pages). The formatted I/O chapter is long because of the richness of the features that Formatter and Scanner offer.

[ August 26, 2004: Message edited by: Herb Schildt ]


Thanks a lot for such detailed info about the chapters of the book...

As you said that Formatter I/O chapter is long and it got the rich features in it, will it be such long that Max Habibi can later come up with a new edition of his book Java Regular Expressions?
C Kutler
Ranch Hand

Joined: Apr 15, 2004
Posts: 62
Ko Ko

Thanks for the URL to the article . I happen to be working on a class with a non-typesafe enum workaround (public static final Suit) and didn't know about this better way.

Coming from C/C++ I have missed enums.


We learn by doing, there is no other way.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Enumerations