I am writing in Java and trying to create a method that will take any Enum class that I pass to it, as described here (mostly): Java: Generic method for Enums
I am getting a problem when I try to pass an enum into this type of method. It wants me to change the method to take a specific Enum class. Does anyone know what I am doing wrong?
Here is my version of the method:
public Class Presenter{
public static <E extends Enum<E>> List<String> getEnumString(Class<E> eClass){
List<String> returnList = new ArrayList<String>();
for (E en: EnumSet.allOf(eClass)){
returnList.add(en.name());
}
return returnList;
}
}
and here is what I am attempting to pass in. The error is saying that the argument is not applicable, and it suggests changing the method above to only take MyEnumClass:
MyEnumClass eclass;
List<STtring> string = presenter.getEnumString(eclass);
I thank anyone who can help in advance.
Thanks!
-Eli
eClass is a reference to a MyEnumClass object. What you need to pass into your function is the class type itself. The syntax is:
The following code will do what you want. Notice I pass in MyEnumClass.class, not a variable.
Output: