HOWTO · Java

Java - Use NotNull Annotation in Method Argument

Learn how to write @NotNull on a Java method argument and what the annotation communicates. The declaration expresses a non-null contract; a separate configured mechanism is needed to enforce it at runtime.

On this page

Use @NotNull on a reference-type method argument to document that callers are expected to provide a value. The annotation communicates a contract; by itself, it does not reject null or run validation. This example shows the declaration and explains what additional enforcement means.

What is the @NotNull Annotation?

@NotNull is an annotation used by Java libraries to express that a value is expected to be non-null. It can make an API contract easier to see in source code, documentation, and tools that understand the particular annotation. The exact package and behavior depend on which library provides it, so use the annotation supported by your project rather than assuming every @NotNull has the same implementation.

An annotation declaration does not change Java’s type system or prevent a variable from holding null. It also does not guarantee that a method returns a non-null value or that passing null will cause an exception. Runtime enforcement requires a separate mechanism configured to inspect or validate the annotated value; that mechanism and its behavior are outside this declaration-only example.

The main benefit is clearer communication between the method author and its callers. A visible non-null contract can help a reader understand the intended input and can support compatible static analysis or framework tooling. It is useful documentation, but it should not be treated as a substitute for checking inputs where the application needs enforcement.

Using @NotNull in Method Arguments

Place the annotation immediately before a reference-type parameter, such as String. The following is a declaration-only illustration; select the import and library that your project uses:

public void createUser(@NotNull String username) { ... }

Here, username is the method argument and @NotNull expresses that callers are expected to supply a non-null string. String is a reference type, so it can receive null in Java unless some separate check or configured mechanism rejects it. Primitive types such as int cannot receive null, so they do not demonstrate this contract.

If your project needs enforcement, configure and invoke a mechanism that supports the specific annotation you chose. Merely adding an annotation import or writing it on the parameter does not perform validation. The declaration above has no dependency, provider, invocation path, or runtime result associated with it; those choices vary by project and are not shown here.

Use the contract to make intent easier to review and maintain. Callers should pass a real username, and the method can still apply its own input checks when required by the application. Keep the annotation’s package consistent across the codebase, since annotations with the same simple name may come from different libraries and may have different tooling support.