Just before creating you object, you could check the params using the word assert
, i.e :
assert yourValue != null
By default, assertions are disabled at runtime. To enable them, pass the option -enableassertions
(or -ea
) to the JVM.
Another possibility (since Java 7) is to use the method requireNonNull
direclty in your constructor/method, here's an example with the constructor :
public Foo(Bar bar, Baz baz) {
this.bar = Objects.requireNonNull(bar, "bar must not be null");
this.baz = Objects.requireNonNull(baz, "baz must not be null");
}
Otherwise as @Woot4Moo said you could just use an if
statement.