• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generic method in enum. Possible?

 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to do the following.

Is it possible to have a generic method in an enum? If so where/how do I specify the type? I've tried several different ways, but no luck getting anything to compile.

Apparently a <T> in front of a constructor for the enum compiles, but I don't seem to be able to use it.

Any ideas?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way would be to make T a type parameter to the method. That works especially well when the type depends on a method parameter, for example

public <T> T someMethod(Class<T> clazz){

Would that fit your needs?
[ February 24, 2007: Message edited by: Ilja Preuss ]
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That could work, but I'd rather be able to supply it to the enum. Each enum has it's own class type, and it may end up being used in more than one method.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, maybe that doesn't work.

What I have is:


What I'd like is for the toObject method to return the same type as the class passed into the constructor.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Seems to me like it isn't possible, but I might be wrong.
 
reply
    Bookmark Topic Watch Topic
  • New Topic