Enums are one of the features I missed when moving from C++.... Can you tell us a bit about your coverage of these in the new book?
Joined: May 15, 2001
How do you compare enums when using them remotely over RMI?
Joined: Sep 01, 2004
Enums are, in terms of usage, a third construct, in addition to classes and interfaces. At an implementation level, that's not quite true -- but at an implementation level, an interface is treated as a class as well, so this is nothing new to Java folks.
In terms of what I cover, I treat Enums pretty thoroughly. It was actually the first chapter I wrote, and was pretty exhaustive. Here's what the labs deal with:
Creating an Enum - What is an enum? - How does it work at an implementation level? - How is it similar/different from constants like public static final SOME_CONST = 28?
Inline Enums vs. "Standalone" Enums (where the Enum is its own class file)
Iterating over Enum values - Using enums with the new Tiger for/in loop
Switching on Enums (using case and switch) - Handling added values with the default case
Building out Your Enums - Adding methods - Value-specific method bodies
Implementing interfaces with Enums
I also cover some of the "can't do"s, like extending an enum or trying to create one "manually". So I think the coverage is pretty throrough -- of course, I'm biased :-)
Series Editor, Head First<br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596102259/newinstance-20" target="_blank" rel="nofollow">Head Rush Ajax</a> and <a href="http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678/ref=pd_bbs_sr_1/104-5348268-5670331?ie=UTF8&s=books&qid=1192568453&sr=8-1/newInstance-20" target="_blank" rel="nofollow">Head First OOA&D</a>
Joined: Sep 24, 2003
I also cover some of the "can't do"s, like extending an enum or trying to create one "manually".
How can you not create one manually? I have been using type-safe enums for ... well, since I read Bloch's book (maybe 3 or so years ago). My understanding (which is a little vague) is that enums are merely compiled to type-safe enums in the bytecode.
In Java 1.5 enum is a keyword allowing you to define a class-like object (with behavior and state, if desired) ... it looks to be far more powerful than the C++ enums....assuming they're used appropriately
The question is "How can you not create one manually?" The concept of "manual enum creation" to me, is writing the source to a type-safe enum, which I have done for years. I don't understand why the introduction of J2SE 5.0 enums prevents me from doing this (although it would be a bit silly).
I arrived at this definition because an 'enum' is compiled to bytecode as if it were a type-safe enum, as far as I know anyway.
So my question still stands.
Joined: Feb 18, 2004
I think you're right - nothing prevents you from continuing to use current practices to create your own enum. The new language feature just makes it simpler....the "automatic" part is done by the compiler which now knows about the enum keyword and does it's magic to make it work.
In its simplest form I think using the enum keyword is the way to "not create one manually"....