Constructor call with condition

I would like to do something like this:

if (condition) super(foo.class); else super(bar.class); 

But the superconstructor should be the first in the constructor. Is it possible to do this?

+6
source share
1 answer

Assuming you call the same superconstructor in both cases and just pass a different argument, you can simply use the conditional operator:

 super(condition ? Foo.class : Bar.class); 
+11
source

All Articles