Annotation Interface NotNull


An element annotated with NotNull claims null value is forbidden to return (for methods), pass to (parameters) and hold (local variables and fields).

Apart from documentation purposes this annotation is intended to be used by static analysis tools to validate against probable runtime errors and element contract violations.

If a field is annotated as @NotNull it's expected to be initialized during object construction. Tools may issue a warning if it's not the case.

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.

See Also:
  • Element Details

    • value

      String value
      Returns:
      Custom exception message
      Default:
      ""
    • exception

      Class<? extends Exception> exception
      Returns:
      Custom exception type that should be thrown when not-nullity contract is violated. The exception class should have a constructor with one String argument (message).

      By default, IllegalArgumentException is thrown on null method arguments and IllegalStateException — on null return value.

      Default:
      java.lang.Exception.class