Take the 2-minute tour ×

Suppose there is a requirement to use a particular generic interface, but the situation does not require making use of one of the generic parameters.

Let's say I need a Callable<T> (which must return a T from its call() method), but on this occasion I don't need the return result, I just want to submit some code to an ExecutorService to "do something"

What's the best option for the type T?

share|improve this question
+1 It's raining hats questions on stackoverflow :P –  Raffaele Dec 26 '12 at 18:41
@Raffaele Was it that obvious? lol –  Bohemian Dec 26 '12 at 18:47
It was :P especially from 0.05% top users eheh –  Raffaele Dec 26 '12 at 18:50
add comment

3 Answers

up vote 7 down vote accepted

You can use the special Void type:

Callable<Void> callable = new Callable<Void>() {
    public Void call() throws Exception {
        // do stuff
        return null;

The return statement is required to exit the method. The only value which the compiler accepts is null. Quite handy!

share|improve this answer
+1 This is the exact use case for the Void class. –  Paul Bellora Dec 26 '12 at 18:09
the same is true of, say, Math –  newacct Dec 26 '12 at 20:36
add comment

Use Void.

30char30char30char, sorry, not much to add :P

share|improve this answer
add comment

In fact you can also submit a Runnable to an ExecutorService, which does not have a return type.

share|improve this answer
add comment

Your Answer


