• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Generic Enum Declaration - meaning and consequence  RSS feed

Md Sayem Ahmed
Posts: 1
C++ Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, the enum class is declared like this -

I was trying to understand the <E extends Enum<E>> part in the declaration. After reading this post from stackoverflow and this Angelika Langer faq, I understand that it's declared this way because the supertype wants to return object of subtypes from its method implementation (from the getDeclaringClass method), and also it wants to take a subtype as its argument in the compareTo method. As a result, it bound the parameter to itself. Also, the first answer of that stackoverflow post says -

It means that the type argument for enum has to derive from an enum which itself has the same type argument. How can this happen? By making the type argument the new type itself.

I don't understand this part. It says that because of the way the type parameter is declared, the new type should pass itself as a type parameter when extending from Enum class. But what's stopping me to pass another subtype of enum as a parameter instead of the new type being defined, like this -

So recursive generic bounds like this only ensures that the type argument that is passed to it are its subtypes, but it doesn't prevent mixing of its subtypes like I did in the second example.

Am I right, or I am missing something here?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!