If you use Java 8, you should use OptionalBoolean instead of Boolean. It is not as good as its companions Optional<>
, OptionalInt
, OptionalDouble
etc., but in this case, it might be a real benefit.
As the mentionned OptionalBoolean is indeed from com.sun.javafx.scene.control.behavior.OptionalBoolean
, there might be a better alternative.
I personally try to refrain from using null
where there are better options.
Instead of
class Foo {
Bar s; // whatever Bar is...
Foo() {
Foo(null)
}
Foo(Bar s) {
this.s = s;
}
public void doStuff() {
if (s != null) {
bar.frobnicate();
}
}
}
it might be better to have
class Foo {
Bar s; // whatever Bar is...
Foo() {
Foo(Bar.EMPTY_VALUE)
}
Foo(Bar s) {
this.s = s;
}
public void doStuff() {
bar.frobnicate();
}
}
with Bar
having a
public final static EMPTY_VALUE = new Bar() {
// redefine it so that frobnicate() just does nothing.
// This way, EMPTY_VALUE can be used wherever appropriate
}
This is called the Null Object pattern. While it doesn't directly address your problem, it resolves the ambiguities you have by using null
here and there.
Another way to resolve them could be to have every constructor call the "main constructor" directly.
This way you could have
foo(a, b) { return foo(a, b, null, null); }
foo(a, c) { return foo(a, null, c, null); }
foo(a, b, c) {return foo(a,b,c,null);}
foo(a, b, d) {return foo(a,b,null, c);}
foo(a, b, c, d) { implement of the function...; }
It might as well be clearer to read.