• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I cannot understand the Enum class declaration

 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw it here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html

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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic