Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I cannot understand the Enum class declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I cannot understand the Enum class declaration" Watch "I cannot understand the Enum class declaration" New topic

I cannot understand the Enum class declaration

Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
I saw it here:

public abstract class Enum<E extends Enum<E>>extends Objectimplements Comparable<E>, Serializable { ..... }

My question is how to understand "Enum<E extends Enum<E>>".
Thanks a lot
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You are not the only one
Let's try and find out...

This is what David Flanagan writes in O'Reilly's Java in a Nutshell (5th Edition):

Finally, let's turn our attention to the java.lang.Enum class. Enum serves as the supertype of all enumerated types (described later). It implements the Comparable interface but has a confusing generic signature:

public class Enum<E extends Enum<E>> implements Comparable<E>, Serializable

At first glance, the declaration of the type variable E appears circular. Take a closer look though: what this signature really says is that Enum must be parameterized by a type that is itself an Enum. The reason for this seemingly circular type variable declaration becomes apparent if we look at the implements clause of the signature. As we've seen, Comparable classes are usually defined to be comparable to themselves. And subclasses of those classes are comparable to their superclass instead. Enum, on the other hand, implements the Comparable interface not for itself but for a subclass E of itself!

I'm not sure that I understand it yet...
[ October 05, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: I cannot understand the Enum class declaration
jQuery in Action, 3rd edition