Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unneeded generic parameter

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?

like image 843
Bohemian Avatar asked Dec 26 '12 18:12

Bohemian


1 Answers

You can use the special Void type:

Callable<Void> callable = new Callable<Void>() {
    @Override
    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!

like image 139
cambecc Avatar answered Oct 11 '22 10:10

cambecc