Voids in Java Generics, What Works and Why

How Java.lang.Void is implemented in Java. With a private constructure. This class is a wrapper for primitive type void.

	package java.lang;
	public final class Void {
		public static final Class<Void> TYPE = Class.getPrimitiveClass("void");
		private Void() {}

I was seeing a lot of this action and it was confusing me for a while.

	... = new Constructor<Void>()
	... = new Class<Void>()
	... = new Callable<Void>()
	... = new Future<Void>()

It appears java.lang.Void is useful in Generics when methods return Void, and for wildcards that aren't needed but must be set to some type so that the class can be created. Like a situation where you want to use some functionality off a class but don't need that class to be of a certain type.

	public class Cart<T> {
	  private T t;
	  public void add(T t) {
	    this.t = t;
	  public T get() {  // Look at this function.
	    return t;
	  public void doSomething() {
		System.out.println("This couldn't be done unlesst Cart<SomeType> was created");
	public static void main(String [] args) {	
    	Cart cart = new Cart<Void>();

Why java.lang.Void instead of void? It's like when you define an Array of integers using generics you don't create an Array like so "new Array<int>()" you use an Integer object like "new Array<Integer>()". Same with void when you are using generic wildcards to specify the return value of a function you don't use "new Future<void>" you use "new Future<java.lang.Void>()". Make sense now?

Need Help?

Because I like writing code I would be happy to help you solve problems if you have questions you cannot find the answer to yourself for some reason.

Contact me