Annotation Interface NotNullByDefault


@Documented @Retention(CLASS) @Target({TYPE,PACKAGE}) @Experimental public @interface NotNullByDefault
A meta-annotation applicable to Java class or package, which means that the non-primitive types mentioned in the following contexts are recursively not-null by default:
  • Types of fields
  • Types of method parameters
  • Types of method return values
Recursively not-null means that along with types themselves, the components of array types, the type arguments of generic types and the upper bounds of wildcard types are also not-null.

If a method overrides a superclass method, and the superclass method specifies the nullability on parameter or return type, then the subclass method should specify the same nullability, either directly or indirectly via @NotNullByDefault. The only exception is the covariant return type nullability: if the superclass method is declared to return nullable value, then subclass may declare to return a not-null value.

The tools may issue a warning if the nullability for a subclass method contradicts from the specified nullability of a superclass method.

For newly declared type parameters, the annotation applies to its bounds, including implicit Object bound if no explicit bound is declared. For example, <T> declared under @NotNullByDefault scope means the same as <T extends @NotNull Object>. To reset to default behavior in this case, one should use <T extends @UnknownNullability Object>.

The type parameter references are not affected by @NotNullByDefault. For example:

@NotNullByDefault
 interface Pair<K extends @Nullable Object, V> {
   // Not assumed to be @NotNull; may return null depending on the K instantiation
   K getKey();
   // Returns @NotNull, as implicit upper bound of V is @NotNull Object,
   // so it cannot be instantiated with a nullable type
   V getValue();
 } 

The annotation has no effect on local variables.

Since:
26.0.0
See Also: