Java generics void/Void types
The Void
type was created for this exact situation: to create a method with a generic return type where a subtype can be "void". Void
was designed in such a way that no objects of that type can possibly be created. Thus a method of type Void
will always return null
(or complete abnormally), which is as close to nothing as you are going to get. You do have to put return null
in the method, but this should only be a minor inconvenience.
In short: Do use Void
.
Generics only handles object classes. void and primitive types are not supported by Generics and you cannot use these as a parameterized type. You have to use Void instead.
Can you say why you don't want to use Void
?
When you need to return java.lang.Void
, just return null
.
You can't have primitives in generics so that int
is actually an Integer
. The object Void
is analogous with the keyword void
for generics.