You can use the Array.newInstance() method to achieve something like this; consider
public class Question<K> {
private K[] keys;
@SuppressWarnings("unchecked")
public Question(Class<? extends K> cls, int size) {
this.keys = (K[]) Array.newInstance(cls, size);
}
public K[] getKeys() {
return keys;
}
public static void main(String[] args) {
Question<String> question = new Question<String>(
String.class, 10);
String[] keys = question.getKeys();
keys[0] = "Hello";
keys[1] = "World";
keys[2] = "Goodbye";
keys[3] = "I must";
keys[4] = "be Going";
keys[5] = "Now";
keys[6] = "Parting";
keys[7] = "is";
keys[8] = "such";
keys[9] = "sweet sorrow";
System.out.println(Arrays.toString(question
.getKeys()));
}
}
Here this prints
[Hello, World, Goodbye, I must, be Going, Now, Parting, is, such, sweet sorrow]