• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Any Enum as method parameter

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am wondering what is the best way to have a method parameter as any Enum type?

Should it be like

<T extends Enum<T>> int mthd( Class<T> enum)

Or simply

int mthd( Enum<?> enum)

Thanks!
 
Saloon Keeper
Posts: 15551
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first option takes an enum type. That means you can pass it ChessPiece.Color.class, or PlayingCard.Suit.class or something similar.

The second option takes an enum value. That means you can pass it ChessPiece.Color.BLACK or PlayingCard.Suit.CLUBS.

Which one is correct depends on what your method is supposed to achieve. It's also quite possible that neither option is appropriate.
 
Master Rancher
Posts: 4840
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the first declaration captures the type T, which may be useful - e.g. if you want to return something that uses T.  If you don't find a use for it, then you can probably drop the T notation as being unnecessarily verbose.  But I would usually find a use for it.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan is right.

A better version would be int mthd(Class<? extends Enum<?>> enum), unless the first version should be <T extends Enum<T>> int mthd(T enum).

Which one to use depends on what you prefer to read, and whether or not you need that T inside your method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic