Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Enum type values?

 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to run a static method on a parameterized type. Specifically, I have a class that is parameterized with an Enum. From within the class, I'd like to get to the Enum's values. Here's an example of what I'm trying to do.

The "public T[] values()" method won't compile.
[ December 02, 2008: Message edited by: Max Rahder ]
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i think that you cannot do that not in that way. Because you are trying to return the values of an erased type. When you are gonna execute the method values from the Test.class you're actually returning Object[] and the compiler insert a cast to Marx but inside the method you cannot know that type because of how erasure works.

This it's my approach to what you posted


You could substitute the Parameter T in a Child class of Test and that's the only way that i know, that the ParameterizedType can be inferred, like the example below:


[ December 02, 2008: Message edited by: Nitesh Kant ]
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Angel, I think you're right. I'll do as you suggest and pass the enumeration class reference into the constructor, and save it for use in returning its values, as shown below. Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic