This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cannot understand enum class syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cannot understand enum class syntax" Watch "Cannot understand enum class syntax" New topic
Author

Cannot understand enum class syntax

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1

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


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abhay, are you sure this thing compiles??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:




Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Feb 29, 2008
Posts: 1075
    
    1

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

Joined: Mar 19, 2009
Posts: 68
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.


Power from within.

Failed SCJP 2 times :-(
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Prash Negu
Ranch Hand

Joined: Apr 20, 2009
Posts: 45
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

Joined: Dec 16, 2008
Posts: 1032
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.)


All code in my posts, unless a source is explicitly mentioned, is my own.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Did you check the documentation of Enum class. It has got no values method defined in it...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Jun 08, 2009
Posts: 6
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()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot understand enum class syntax
 
Similar Threads
Extending a enum with enum's type
Enums
Enum
I cannot understand the Enum class declaration
depicting enumeration type in class diagram.