Annotation Interface NotNullByDefault
- Types of fields
- Types of method parameters
- Types of method return values
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: