You cannot create an array of concrete parameterized type, because it is not type safe. The reason being, the concrete parameterized type are not reified, whereas arrays are reified entity.
You can however create an array of raw type, or unbounded wildcard type:
EnumSet<?> mySet[] = new EnumSet<?>[3];
The reason this works is because rawtypes and unbounded wildcard types are reified. So, it's typesafe to create an array with such component type.
By reified, we mean the type information is available at runtime. This is not true with concrete parameterized type, because the type information is lost as a process of type erasure. However, for raw types, there is no type at all that can be lost. Same is true for <?>
types. Since EnumSet<?>
is a supertype of all instantiation of generic type EnumSet<T>
, so there is no specific type information about EnumSet<?>
at compile time that can be lost at runtime.
Another option is to simply create a list. This is far more better than mixing arrays with generic types:
List<EnumSet<MyEnum>> mySets = new ArrayList<>();