IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

@ParametersAreNonnullByDefault

Last modified: 10 August 2022

The @ParametersAreNonnullByDefault annotation helps you define that all method parameters in a class or a package have @NotNull semantic, unless they are explicitly annotated with the @Nullable annotation.

The @ParametersAreNonnullByDefault annotation can be used with a package, a class, or a method.

To use the annotation, add the jsr305 library to module dependencies:

Once you add the JAR to your project, you can start using the @ParametersAreNonnullByDefault annotation. For example, consider the following code:

If you annotate the sort() method with @ParametersAreNonnullByDefault, IntelliJ IDEA immediately recognizes that if statement is extraneous, and reports about the condition that is always true.

However, if you annotate the parameter of the method sort() as nullable, you will see no inspection messages.

https://resources.jetbrains.com/help/img/idea/2022.1/paramnotnull.png
Gif