Should I Always Use Generics?

I created unit test:

new Callable() { @Override public Object call() throws ..... 

I got a warning in Eclipse:

 Callable is a raw type. References to generic type Callable<V> should be parameterized 

Should I write code like:

 new Callable<Object>() 

to eliminate the warning or not? This seems to be just a junit test, and it makes no sense to add extra code ... Thanks.

+6
source share
3 answers

If your operation does not return a value (or there is nothing significant to return). You should use java.lang.Void as a type parameter.

 new Callable<Void>() { public Void call() throws Exception { // do work return null; // <-- This statement is required. } } 
+7
source

Yes, it is good practice to avoid raw types and use generic types. Using Callable<Object> makes it clear that Callable is designed to return any object. Using Callable does not make this clear.

+8
source

Runnable is a good replacement for Callable <Void>.

+3
source

Source: https://habr.com/ru/post/919489/


All Articles