Uses for the Java Void Reference Type

Uses for the Java Void Reference Type?

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.

On Void return type

Void is a class like any other, so a function returning Void has to return a reference (such as null). In fact, Void is final and uninstantiable, which means that null is the only thing that a function returning Void could return.

Of course public void blah() {...} (with a lowercase v) doesn't have to return anything.

If you're wondering about possible uses for Void, see Uses for the Java Void Reference Type?

Why does the Void class exist?

It is required by the Reflection API, to represent the return type of a void method, as the result of java.lang.reflect.Method.getReturnType(). (Stricly speaking this is void.class, not Void.class, but the compiler coerces void.class to Void.TYPE.) It is also used in several other places as shown in the Use page in its Javadoc.



Related Topics



Leave a reply



Submit