Java generics void/Void types

61,497

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.

Share:
61,497
Travis Webb
Author by

Travis Webb

Updated on December 06, 2020

Comments

  • Travis Webb
    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, 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?

  • Travis Webb
    Travis Webb about 13 years
    void is not a primitive, it's merely a java keyword. It has no "type"
  • user85421
    user85421 about 13 years
    and it surely is not synonymous! void (lowercase) - keyword, nothing returned; Void (uppercase) - an (uninstantiable) class, instance of that class being returned (can only be null since uninstantiable)
  • Travis Webb
    Travis Webb about 13 years
    i'd just prefer not to have to include a return statement in a function where I'm not returning anything, for cleanliness mostly.
  • Vishy
    Vishy about 13 years
    You could create an abstract wrapper which call you void method and returns null to hide this ugliness.
  • Travis Webb
    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
    mark-cs about 13 years
    @Carlos Heuberger analogous any better for you ?
  • peterk
    peterk about 11 years
    Arrgh - 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
    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
    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
    Nathan Niesen almost 7 years
    Because 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
    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 of Object or any subclass, such as Void do have to return a value (or not complete normally). The only value that can be assigned to Void, and therefor returned from a Void method, is null.
  • Chaitanya
    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