Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does using @NonNull on a void method do?

Tags:

java

lombok

What's the point in Lombok annotation @NonNull for a method?

class MyClass {
    @NonNull
    void run() {
        // code here
    }
}

Are we check the instance of class MyClass (MyClass obj == null)?

like image 784
barbara Avatar asked Jan 14 '15 16:01

barbara


People also ask

What does @NonNull annotation do?

@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.

How does NonNull work in Java?

The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not. If the passed object is non-null, then the method returns true. If the passed object is null , then the method returns false.

What is the use of @nullable annotation?

On the other hand, if the parameter is mark as @Nullable and we don't add null check inside the function, Android Studio will warn you with lint error and some visual hint. The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability.

IS NOT null method?

notNull() is a static method of the Validate class that is used to check whether the passed object is null or not. If the passed object is null , then the method raises an exception with a formatted message. If the passed object is not null , then the method returns the input as it is.


1 Answers

For a void method, there's no point.

For a method with a return type, it shows that it won't return null:

@NonNull String foo() {
    // I promise I won't return null herre!
}

Judging by the tutorial, it looks like it won't generate a check within the method to ensure that it isn't null, but it tells any caller that they can realistically expect a non-null value to be returned.

like image 141
Jon Skeet Avatar answered Oct 18 '22 11:10

Jon Skeet