23
I saw that in Java 7, they introduced the method Objects.requireNonNull(T obj, String message).
Checks that the specified object reference is not null and throws a customized
NullPointerExceptionif it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters.
Before starting to reformat my code, I would ask here to have some feedback about using that.
public Foo(Bar bar, Baz baz) {
/** Old one
this.bar = bar;
this.baz = baz;
**/
this.bar = Objects.requireNonNull(bar, "bar must not be null");
this.baz = Objects.requireNonNull(baz, "baz must not be null");
}
Is it a better practice to use it directly when I construct my object (I was thinking about if I create a library or other stuff for developers) ?
Or should I leave it as a "classic/old one" constructor ?