Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Passing any enum type to a method

843793Nov 13 2008 — edited Nov 14 2008
Consider I have created some enums:
public enum Day {
    SUN, MON, TUE, WED, THU, FRI, SAT 
}

public enum Month {
    JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC 
}
I want to create a method that will accept any type of enum and display all values. So the call would look something like:
showAll(Day);
showAll(Month);
I'm not sure what to pass to the method. My understanding of Generics is rudimentary at best, despite reading the tutorial several times now. Here is some uncompilable code that might illustrate:
public void showAll(EnumType enumType)  // <--- not sure what to pass here
{
  Enum[] allValues = enumType.values();
  // then, loop over the allValues array and I can proceed from here
}
The actual code is needed is for displaying a list of checkboxes to the user where the input is the enum class, and the checkboxes are every enum value.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2008
Added on Nov 13 2008
7 comments
2,016 views