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

Tiger enums....

Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Hi Brett,

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?

Thanks


Rich
SCJP 1.4
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
How do you compare enums when using them remotely over RMI?
Brett McLaughlin
author
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
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

Collections and Enums
- EnumMap
- EnumSet
- Bitwise operations

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 :-)

Thanks
Brett


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>
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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.

I assume you are referring to something else?


Tony Morris
Java Q&A (FAQ, Trivia)
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
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

Info from Sun on enums
[ October 21, 2004: Message edited by: Richard Quist ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Richard,
Was that in response to my question?

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.
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Tony,

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"....

Or have I completely misunderstood your question?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tiger enums....