I am fully aware that generic arrays cannot be instantiated like such:
data = new Entry<K, V>[];
This will result in an error:
Cannot create a generic array of Entry<K,V>
So, why am I allowed to declare an instance variable that is a generic type array with no errors?
private Entry<K, V>[] data;
In principle, the comment of Joachim Sauer is already your answer, however, I would like to detail it a bit.
Sun (Oracle) knows of a phenomenon called memory-pollution, which always happens if a generic variable pointer points to a type-incompatible object. This can be enforced for example with the following code:
obviously, you will start seeing
ClassCastExceptions, once you start working with that code. This was perfectly ok for Sun when designing Generics, because you get an obligatory warning RawType/Unchecked conversion. Whenever this warning is issued, you know, that you have code, which is not 100% type-checked and memory pollution may occur.The overall design principle in Generics is, that all possible memory-pollutions are indicated by such warnings. This is why the creation of generic arrays is forbidden. Assuming it was not, here is what could happen:
You will have memory-pollution there without any warning and for compatibility reasons, a warning cannot be generated. This is why Sun decided to forbid arrays of generic types. However variables might be declared because you get your unchecked conversion warning there and this is all Sun wanted: a warning if pollution could occur.