File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Generics Enum type values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics Enum type values?" Watch "Generics Enum type values?" New topic

Generics Enum type values?

Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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

Joined: Nov 13, 2008
Posts: 84

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

Joined: Nov 06, 2000
Posts: 177
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!
I agree. Here's the link:
subject: Generics Enum type values?
It's not a secret anymore!