Java

Java

By DeepSource

Constructor must not be marked with nullable annotations JAVA-W1070

Anti-pattern

Nullable annotations such as @Nullable and @CheckForNull on constructors is an antipattern.

In Java, constructors always return an instance of the class in which they are defined. Explicitly returning null from a constructor raises a compile-time error. In Java, it is impossible for a constructor to return null. Therefore, annotations such as @Nullable and @CheckForNull which are typically used to reflect that a method may return null, should be removed from constructors.

Bad Practice

public class Klass {
    @Nullable
    public Klass() {
        // ...
    }
}

Recommended

Consider removing @Nullable and @CheckForNull from constructors. ```java public class Klass { public Klass() { // ... } }

References