What is the correct way to return a Void type, when it isn\'t a primitive? Eg. I currently use null as below.
interface B{ E method();
Just for the sake of it, there is of course the possibility to create Void instance using reflection:
interface B{ E method(); }
class A implements B{
public Void method(){
// do something
try {
Constructor voidConstructor = Void.class.getDeclaredConstructor();
voidConstructor.setAccessible(true);
return voidConstructor.newInstance();
} catch (Exception ex) {
// Rethrow, or return null, or whatever.
}
}
}
You probably won't do that in production.