generic class of enum, number of values

enums, generics, java

Solution

You need to pass in the class literal of the enum:

public Analyser(Class<C> enumType) {
    super();
    dist = new long [enumType.getEnumConstants().length];
}

...

Analyser<MyEnum> analyser = new Analyser(MyEnum.class);

This is because `C` has no meaning at runtime due to type erasure.

Problem

How do I find out, how many values my enum has in this example: ``` public class Analyser<C extends Enum<C>>{ private long[] dist; public Analyser() { super(); dist = new long [C.getEnumConstants().length]; } } ``` The last line does not work.

Original source