A โgeneratedโ array can be built by reflection:
T[] array = (T[]) Array.newInstance(Byte.class, 32)
Replace with the Byte.classlink to the desired class. In other words:
public class TestClass<T> {
T[] array;
@SuppressWarnings("unchecked")
public TestClass(Class<T> type) {
array = (T[]) Array.newInstance(type, 32);
}
public T[] getArray() {
return array;
}
public boolean doThing() {
T[] t = array;
return t == array && t != null;
}
}
You can check this out as such:
public static void main(String[] args) {
TestClass<Byte> test = new TestClass<Byte>(Byte.class);
Byte[] array = test.getArray();
System.out.println(Arrays.asList(array));
}
- , Class<?> .