Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot understand enum class syntax

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I can not understand this class definition syntax. how can the smae class (enum) can be used in genrics syntax

Class Enum<E extends Enum><E>>


Please explain.

Thanks
Abhay
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay, are you sure this thing compiles??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have misplaced some of the brackets; the correct declaration is this:



All enums in Java are subclasses of that class (see its API documentation in package java.lang). It looks a bit strange, because E extends Enum, while this is the declaration for class Enum itself (so it looks like a kind of recursive declaration).

This means that class Enum has a generic type parameter E, and the type E must be a subclass of Enum<E>. It is declared like this to make it so that class Enum can only be used for the purpose it was meant for; you can't, for example, do this:



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like there is a bug in the forum software; it automatically adds the extra > after the Enum when you try to post "class Enum<E extends Enum<E>>". I've notified the other moderators about this.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jesper

thanks for the reply

I was not able to understand this line

"class Enum has a generic type parameter E, and the type E must be a subclass of Enum<E>. It is declared like this to make it so that class Enum can only be used for the purpose it was meant for"

Please explain

Thanks
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the K&B Chapter 7? Everything is explainend quite well in there.

Yust see this as some kind of making type-safe statements.
You're declaring an enum-generic (mybe for legacy code) with an type parameter E which must extend enum.

If the type-Parameter will (why ever) be of type String, for example, the compiler will show you an error, cause E must be of an subtype of enum.
This will only work on compiletime.

On runtime (as far as i remember) generics are treated as objects of type Object.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On runtime (as far as i remember) generics are treated as objects of type Object.


Compiler make sure the correct types are applied, so at run time no type information is kept (i.e: Type erasure).
 
Prash Negu
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Where is the implementation of equals(), hashCode() and values() methods of any enum. Are these implemented in a new class that compiler creates out of enum defination which also extend java.lang.Enum?


Thanks,
Prashant
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prash Negu wrote:Hi,

Where is the implementation of equals(), hashCode() and values() methods of any enum. Are these implemented in a new class that compiler creates out of enum defination which also extend java.lang.Enum?


Thanks,
Prashant

Prash,

I'm not completely sure about this, but my guess is that:
- The equals() and hashCode() are just inherited from Object.
- The values() calls are resolved at compile time to a specific array for each enum, given than once the enum has been declared this array will be known (since you can't create additional instances of enum at runtime.)
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the documentation of Enum class. It has got no values method defined in it...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Did you check the documentation of Enum class. It has got no values method defined in it...

I know, Ankit. That's why I said that this must be resolved at compile time (in other words, everytime the compiler sees a call to values() it must substitute it directly by the array (which is known at compile time, since you can't add new values to an enum outside of its declaration, which is also known at compile time.) But maybe I'm wrong, I'm just guessing.

And to add a little note, the JLS mentions that enums have an "implicit" values() method. The way that sounds to me is that such method doesn't even exist, and is just a notation for the compiler to substitute the array.
 
Teven Chan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
values() is a static values method. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. Just as an example, like here:

http://java.sun.com/javase/6/docs/api/java/lang/Thread.State.html#values()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic