In the generic class Class<T>
the method getConstructors()
has a return type with unknown generic type parameter instead of T
. The reason for this is explainend in the javadoc.
Note that while this method returns an array of
Constructor<T>
objects (that is an array of constructors from this class), the return type of this method isConstructor<?>[]
and notConstructor<T>[]
as might be expected. This less informative return type is necessary since after being returned from this method, the array could be modified to hold Constructor objects for different classes, which would violate the type guarantees ofConstructor<T>[]
.
A colleague of mine and I have tried to understand that explanation. In our understanding they are basically saying that it is of unknown generic type, because some caller could put other Constructor
objects into that array. Did we get that right? And if so, why would someone design an API this way. Wouldn't it be better to use the specific type and trust the programmer to use the array correctly? To us it sounds a little like "We are making a worse API because the programmer using it might try something stupid". Where lies our fallacy?