Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4292

Why doesn't @NotNull raise a compile time error?

I just came across annotations , and I started using them. I made an interface which had a method signature with a @NotNull param like

 interface {
    public void test(@NotNull CustomObject c );
}

I implemented the method in a class

clazz implements interface
{
@Override
public void test(@NotNull CustomObject c)
{
}
}

I expected new Clazz().test(null) to complain while i compiled , but it is not . I am using Android studio , which is built on Intellij .

If these annotations are not supposed to give us compile time errors , then what are they used for ?

Upvotes: 2

Views: 1507

Answers (1)

PedroAGSantos
PedroAGSantos

Reputation: 2346

I think you are using it for the wrong porpouse.

@NotNull

The @NotNull Annotation is, actually, an explicit contract declaring the following:

A method should not return null. A variable (like fields, local variables, and parameters) cannot hold null value. For more information and code examples, refer to online how-to.

IntelliJ IDEA warns you if these contracts are violated.

Please check the resource link for more info https://www.jetbrains.com/help/idea/2016.1/nullable-and-notnull-annotations.html

Upvotes: 1

Related Questions