I didn’t even know this was doable, but I saw while perusing some code online a method with a signature like this:
public List<Void> read( ... )
… What? Is there ever a reason to do this? What could this List even hold? As far as I was aware, it’s not possible to instantiate a Void object.
It is possible that this method signature was created as a by-product of some generic class.
For example,
SwingWorkerhas two type parameters, one for final result and one for intermediate results. If you just don’t want to use any intermediate results, you passVoidas the type parameter, resulting in some methods returningVoid– i.e. nothing.If there were a method
List<V> returnAllIntermediateResults()inSwingWorkerwithVoidas the type parameterV, it would have created a method just like you posted in your question.The code would be perfectly valid. You can instantiate any implementation of the
Listinterface (e.g.ArrayList) with type parameterVoid. But the only value aVoidtype can have isnull. So the list could not hold anything else butnulls, if the implementation allowsnullelements.