Uses for the Java Void Reference Type?

Julien Chastang picture Julien Chastang · Mar 13, 2009 · Viewed 81.7k times · Source

There is a Java Void -- uppercase V-- reference type. The only situation I have ever seen it used is to parameterize Callables

final Callable<Void> callable = new Callable<Void>() {
            public Void call() {
                foobar();
                return null;
            }
        };

Are there any other uses for the Java Void reference type? Can it ever be assigned anything other than null? If yes, do you have examples?

Answer

Tom Hawtin - tackline picture Tom Hawtin - tackline · Mar 13, 2009

Void has become convention for a generic argument that you are not interested in. There is no reason why you should use any other non-instantiable type, such as System.

It is also often used in for example Map values (although Collections.newSetFromMap uses Boolean as maps don't have to accept null values) and java.security.PrivilegedAction.

I wrote a weblog entry on Void a few years back.