Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumerations

 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic