You call B
inherits the generic from the outer class, as it is not static. And you can't just make it static, because it will then need E
also.
So your B.b
array will indeed need a type that is generic, i.e. A<E>.B
or if you'd change your code to a static inner class, A.B<E>
(if you would use private static class B<E>
).
In Java, due to the way generics are implemented (by erasure), the type of the array is not well-defined. On one hand, it should be an array of B
, on the other hand, it should be an array of Object
.
The most workable solution seems to be to use Object[]
and cast explicitly.
If you want increased type safety, you can of course use an ArrayList<B>
, which internally uses Object[]
, too!
In you particular code, B b1, b2;
might also be an option which is actually faster (no bounds checking) and needs less memory (no array object; no size information).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…