r/java • • Aug 12 '24

JSpecify 1.0.0 and Nullability in Java

https://www.infoq.com/news/2024/08/jspecify-java-nullability/
52 Upvotes

33 comments sorted by

View all comments

5

u/uniVocity Aug 12 '24

“The primary use cases are expected to be variables, parameters and return values, but they can be used anywhere that tracking nullness makes conceptual sense.”

I’d really love to be able to annotate a class as @NotNull, or implement some NotNull interface that won’t allow my code to compile if any instance of this class or subclasses could be passed around as null.

This would reduce the need to litter method parameters with @NotNull, or to add the suggested ? or unless necessary (when classes aren’t declared as notNull)

5

u/kevinb9n Aug 12 '24

It's actually very rare to need to write `@NonNull`. We almost didn't even include it in the jar! Just put `@NullMarked` on your class/package/module instead.

The feature you seem to be asking for was discussed a bit in https://github.com/jspecify/jspecify/issues/7 but overall it just doesn't have that many advantages.

2

u/[deleted] Aug 13 '24

Are there any examples available that show how to specify @NullMarked at the package or module level? I've combed through the JSpecify docs, but haven't found any.

3

u/kevinb9n Aug 14 '24

You put it before `package` in `package-info.java`, or before `module` in `module-info.java`.