Java generics void/Void types
Solution 1
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
?
Solution 2
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
.
Solution 3
When you need to return java.lang.Void
, just return null
.
Solution 4
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.
Travis Webb
Updated on December 06, 2020Comments
-
Travis Webb over 3 years
I am implementing a
ResponseHandler
for the apache HttpClient package, like so:new ResponseHandler<int>() { public int handleResponse(...) { // ... code ... return 0; } }
but I'd like for the
handleResponse
function to return nothing, i.e.void
. Is this possible? The following does not compile, sincevoid
is not a valid Java type:new ResponseHandler<void>() { public void handleResponse(...) { // ... code ... } }
I suppose I could replace
void
withVoid
to return aVoid
object, but that's not really what I want. Question: is it possible to organize this callback situation in such a way that I can returnvoid
fromhandleResponse
? -
Travis Webb about 13 years
void
is not a primitive, it's merely a java keyword. It has no "type" -
user85421 about 13 yearsand it surely is not synonymous!
void
(lowercase) - keyword, nothing returned;Void
(uppercase) - an (uninstantiable) class, instance of that class being returned (can only benull
since uninstantiable) -
Travis Webb about 13 yearsi'd just prefer not to have to include a
return
statement in a function where I'm not returning anything, for cleanliness mostly. -
Vishy about 13 yearsYou could create an abstract wrapper which call you
void
method and returnsnull
to hide this ugliness. -
Travis Webb about 13 years+1 for reminding me that "any problem in computer science can be solved by adding a layer of indirection"
-
mark-cs about 13 years@Carlos Heuberger analogous any better for you ?
-
peterk about 11 yearsArrgh - the pain of generics the issue is if lets say you define a return type of "Void" you must put in a "return(null);" the main reason to declare the return type of void is so you don't have to have a return statement, and can use an existing "void" method :) Part of the issue with Java Generics is the conflation of run-time type checking with the need for compile time code generation and "bookeeping" assistance. One needs "macros" and "templates" that offer convenience in writing code on the language side.
-
John McCarthy over 10 years+1 To clarify, Void has been around since JDK1.1. It was originally created for use with Reflection but has a similar role in generics.
-
Andriy Simonov over 7 years@ILMTitan Could you please give any references to the documentation about "return null" statement in methods that defined to return Void?
-
Nathan Niesen almost 7 yearsBecause with a return type of Object the compiler cannot guarantee that only null is returned. Expanding on Thomas' answer, try these:
static Void v() { return null; }
static Void n() { return "NotAllowed"; }
static Object o() { return "Allowed"; }
-
ILMTitan over 6 years@andriy-simonov Methods with a return type of
void
do not have to return a value. Methods with a return type ofObject
or any subclass, such asVoid
do have to return a value (or not complete normally). The only value that can be assigned toVoid
, and therefor returned from aVoid
method, isnull
. -
Chaitanya about 4 years@TravisWebb Looking at your specific need, just don't use generics at all and return
Object
type if it'll work