Search code examples
javavoidreturn-type

Java generics void/Void types


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?


Solution

  • 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?