I don't feel satisfied by avoiding null in OOP. Is there any alternative solution? I don't like to avoid it in this way either.
What is the best possible way to handle it?
Java 8 has the new Optional
type which is what I think you are asking about
A container object which may or may not contain a non-null value. If a value is present,
isPresent()
will return true andget()
will return the value.
Although you don't like it, the Null Object Pattern is one of the best ways to avoid null. Also never return null when you could return an empty list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With