Java generics void/Void types

后端 未结 6 1316
天命终不由人
天命终不由人 2020-12-08 09:36

I am implementing a ResponseHandler for the apache HttpClient package, like so:

new ResponseHandler() {
    public int handleResponse         


        
6条回答
  •  时光取名叫无心
    2020-12-08 09:48

    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.

提交回复
热议问题