Java - Use NotNull Annotation in Method Argument

MD Aminul Islam Oct 12, 2023
  1. Overview of @NotNull Annotation
  2. Use @NotNull Annotation in Method Argument
Java - Use NotNull Annotation in Method Argument

Today’s tutorial discusses the @NotNull annotation and uses code examples to demonstrate how we can use it in method arguments while working in Java.

Overview of @NotNull Annotation

If you want to set the variable or method not to return any null value, then you may use the @NotNull annotation. It is a standard JSR annotation and validates whether the annotated property value is not null.

The specialty of the @NotNull is if you set a method to @NotNull, it will never return any null value, and if a variable is set to @NotNull, it can not hold the null value.

The critical point is that if the parent method contains the @NotNull, then its child needs to be annotated with @NotNull. So next, let’s jump to code fences below to learn its use in method argument.

Use @NotNull Annotation in Method Argument

Example Code:

import javax.validation.constraints.NotNull;

public class JavaNotNull {
  int sum(@NotNull int a, int b) {
    return a + b;
  }

  public static void main(String args[]) {
    JavaNotNull jnn = new JavaNotNull();
    System.out.println("The sum is: " + jnn.sum(5, 5));
  }
}

Output:

The sum is: 10

In the above example, we first included the required package for the @NotNull as import javax.validation.constraints.NotNull;, after that, we created a method named sum() where we set its value as @NotNull.

Note that the sum() method will return the result of the sum operation between two variables. Then we created an object of the JavaNotNull class and called its method sum() to get the above output.

Are you thinking about what happens if we try to pass a null variable to the sum() of the above example?

The answer is that we set the method variable to @NotNull in our above example, so it cannot receive the null value. In the following updated example, we will try to pass a null value to the sum() method to see what happens.

Updated Example Code:

import javax.validation.constraints.NotNull;

public class JavaNotNull {
  int add(@NotNull int a, int b) {
    return a + b;
  }

  public static void main(String args[]) {
    JavaNotNull jnn = new JavaNotNull();
    // We are trying to pass a null value here
    System.out.println("The sum is: " + jnn.add(null, 5));
  }
}

Now, if you try executing the above code example, you will get the below error. Because as we set the method to @NotNull, it cannot receive null values.

error: Class names, 'JavaNotNull', are only accepted if annotation processing is explicitly requested
1 error

An important note is if you call a method with a null value and the method is set to @NotNull. It may cause the NullPointerException at runtime.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn