java.lang.Void

VoidI’ve considered myself a Java guy (that’s before I recently started with Ruby). I’ve stumbled upon Java in 1998 and fell in love with it at first sight. I remember quite crisply my first encounter of the third kind with Hello World. But I confess, I only learned about java.lang.Void yesterday. I was digging through the SwingWorker concepts and as I went through the tutorial I was intrigued by a mention about the Void type in the Simple Background Tasks section.
I already experimented with the SwingWorker before reading the tutorial and liked it except for the annoying return and interim results types as I’m migrating an ancient Java 1.1.8 GUI to JDK 6 at work and those Runnable classes I’m thinking of converting to don’t have any return types.

In short, java.lang.Void is a class representing the void type (non-instantiable, mind you). I should have known, all primitives have class.<primitive> representations and the reflection mechanism should have a representation of void too. Well, I didn’t.

The nice thing about this little-known-to-me class is that it fits like the proverbial Cinderellian shoe straight into the generics of SwingWorker (and elsewhere now that I know about it). From the example:

SwingWorker worker = new SwingWorker<ImageIcon[], Void>();

In other words, if you don’t have a return type for your SwingWorker::done() method just slap a java.lang.Void and you’re saved.
[Copyrighted image courtesy of toulcophoto]