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.
public class Klass {
@Nullable
public Klass() {
// ...
}
}
Consider removing @Nullable
and @CheckForNull
from constructors.
```java
public class Klass {
public Klass() {
// ...
}
}