Is it possible to do something like class Foo<@NotNull T>
or new Foo<@NotNull Bar>
where the first one states that for all instances of Foo
the value in the generic cannot be null and the second one states that for that instance of Foo
it's generic value cannot be null?
Asked
Active
Viewed 270 times
0

vandench
- 1,973
- 3
- 19
- 28
-
1What do you mean _cannot be null_? Do you mean absent, as in a raw type? – Sotirios Delimanolis Dec 10 '16 at 15:50
-
As in if I were to have a list any value added to the list cannot be null. – vandench Dec 10 '16 at 15:56
-
3That's not something you can do with type declarations. Enforce it wherever a value of the type parameter is expected. – Sotirios Delimanolis Dec 10 '16 at 16:02
-
nulls are baked into the language. null is a valid reference to pass in to any method that accepts an object. – Bohemian Dec 10 '16 at 16:08
-
related: http://stackoverflow.com/questions/6433478/is-there-a-standard-java-list-implementation-that-doesnt-allow-adding-null-to-i – jaco0646 Dec 10 '16 at 18:48