The following code gives a "generic array creation" error.
public class TestClass<K, V> {
Entry[] entry;
private TestClass() {
entry = new Entry[10]; // <--- this line gives generic array creation error
}
private class Entry {
public Entry() {
}
}
}
I'm wondering why this is, because class Entry
is not a generic class and has no objects of generic type.
Is it because the inner class still has access to the generic types, even if it doesn't use any? That's the best I can come up with, though if it were the case, I don't understand why Java couldn't look and see it makes no use of generic types and is therefore not a generic class?
And yes, I have seen many many threads about generic type arrays, but no, I have not found a single one regarding inner classes.
The type is actually TestClass<K, V>.Entry
(yes it's because it's an inner class). You can solve this by transforming it into a nested static class:
private static class Entry {
public Entry() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With