Is there a clean way of passing an enumeration CLASS to a function?
Consider something like this:
public enum MyEnum implements XYZ {
//enum values
A,
B,
C;
}
public void getEnumeration(final Class<? extends XYZ> myEnum) {
//go through enumeration
for (XYZ xyz : myEnum.getEnumConstants()
System.out.println(xyz.toString());
}
enumeration passed in as follows:
getFunction(MyEnum.class);
now… the above works and I can pass my enumeration to the getEnumeration() function just fine. HOWEVER, I’d like to ensure that what I am passing in is indeed an enumeration and not any class that implements XYZ. I realize that enumeration is a type of class, but is there a way to strong type this such that the compiler would pick it up if I pass in a class that implement XYZ instead of enumeration that implements XYZ?
You can do this:
Here’s an actual example I wrote to test out my concept: