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, since void
is not a valid Java type:
new ResponseHandler<void>() {
public void handleResponse(...) {
// ... code ...
}
}
I suppose I could replace void
with Void
to return a Void
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 return void
from handleResponse
?
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
.