If you’re using generics*, you always need to include both types. Depending on your specific use, there are a couple of ways you could do this:
-
Use a wildcard for one of them:
Foo<?, Integer> foo = new Foo<>(69);
-
Use the
Void
type:Foo<Void, Integer> foo = new Foo<>(69);
Both of these will prevent you from passing any value for A
except for null
. Return types for the first approach will be of type Object
. For the second solution, they will be Void
, which can only be null
**, because it has no public constructor.
*You can use a raw type and specify no type parameters, but this is strongly discouraged.
**Unless you use reflection to instantiate it.
solved Java – If I specify multiple generics, do I have to use them all?