Why You Can't Create a Generic Array in Java — Reifiable vs Non-Reifiable Types
Fast recall
Quick revision
Core answer
new T[], the array would at runtime be a plain Object[] that wouldn't enforce the T constraint — you'd get silent corruption until a ClassCastException on retrieval. Reifiable types (primitives, Object, raw types, ?-unbounded wildcards) have complete runtime type info. Non-reifiable types (T, List, List extends Number>) don't. Workarounds: declare as Object[] with a cast plus @SuppressWarnings("unchecked"), or pass a Class and use Array.newInstance(clazz, size).Whether the candidate understands that arrays are covariant and reified while generics are invariant and erased — two incompatible models Java chose to keep separate
Blaming 'just erasure' without knowing arrays do a runtime store check (ArrayStoreException). Not knowing the Array.newInstance() workaround. Confusing reifiable vs non-reifiable
Explain the ArrayStoreException on arrays vs the compile-time check on generics — two different safety models. Mention that `@SafeVarargs` was added to suppress the generic-varargs warning because the same issue bites you there
Interview-ready guide
Interview answer
Arrays and generics live in two incompatible worlds. Arrays are reified and covariant — they know their element type at runtime and String[] is a subtype of Object[]. Generics are erased and invariant — at runtime List is just List, and List is NOT a subtype of List.
Why `new T[]` is banned
- If allowed, the array would at runtime be a bare
Object[]— noTenforcement - Because arrays are covariant, you could assign it to
Object[]and store anything in it - The generic type safety would silently collapse; you'd get
ClassCastExceptionor wrong values on retrieval - Java closes this loophole at compile time instead
Reifiable vs non-reifiable types
- Reifiable: primitives, non-generic types, raw types, unbounded wildcards — arrays of these are legal
- Non-reifiable:
T,List,List extends Number>— arrays of these are compile errors - Rule: if the full type information survives erasure, arrays work; otherwise they don't
Workarounds
- Internal storage:
(T[]) new Object[n]with@SuppressWarnings("unchecked")— fine as long as the array doesn't leak out of the class - Returnable array:
Array.newInstance(clazz, n)— creates a realString[]at runtime using the Class token - Best option in application code: just use
ArrayList
The varargs trap
- Varargs secretly create an array:
T... itemsbecomesT[] itemsin bytecode - Same non-reifiability issue; compiler warns about "heap pollution"
@SafeVarargs(Java 7) suppresses the warning when the method author guarantees no unsafe use
Recommendation: In application code, use List or ArrayList — you almost never need a generic array. For library code where you must expose arrays, use Array.newInstance(clazz, size) with a Class token — it produces a true runtime-typed array with ArrayStoreException protection. Reserve the (T[]) new Object[n] trick for private internal storage that never escapes the class.
Why You Can't Create a Generic Array in Java — Reifiable vs Non-Reifiable Types
Direct Answer
new T[] is illegal because Java arrays are reified (store their element type at runtime and do a runtime type check on every write) while generics are erased (no runtime type info). If the compiler allowed new T[], the array would at runtime be a plain Object[] that wouldn't enforce the T constraint — you'd get silent corruption until a ClassCastException on retrieval. Reifiable types (primitives, Object, raw types, ?-unbounded wildcards) have complete runtime type info. Non-reifiable types (T, List<String>, List<? extends Number>) don't. Workarounds: declare as Object[] with a cast plus @SuppressWarnings("unchecked"), or pass a Class<T> and use Array.newInstance(clazz, size).
Common Mistakes & Pitfalls
Blaming 'just erasure' without knowing arrays do a runtime store check (ArrayStoreException). Not knowing the Array.newInstance() workaround. Confusing reifiable vs non-reifiable
Check your understanding
Answer these without looking back at the article.
- What is `ArrayStoreException` and why don't generics throw an equivalent at runtime?
- Why are arrays covariant while generics are invariant — what was each language's reasoning?
- When is it safe to use `@SuppressWarnings("unchecked")` with an `Object[]` cast, and when is it dangerous?
- Why did Java 7 add `@SafeVarargs`, and which JDK methods use it?
- How would you implement a generic `toArray(T[] a)` method correctly?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.