Uses for the Java Void Reference Type? Uses for the Java Void Reference Type? java java

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.


You can create instance of Void using reflections, but they are not useful for anything. Void is a way to indicate a generic method returns nothing.

Constructor<Void> constructor = Void.class.getDeclaredConstructor();constructor.setAccessible(true);Void v = constructor.newInstance();System.out.println("I have a " + v);

prints something like

I have a java.lang.Void@75636731