Optional
object's typical use case is for method's return
types only.
Also, it is NOT a good practice to use Optional
as method arguments or constructor arguments because we can't enforce non-null optional objects i.e., the Optional
object itself can be null
. In other words, even if you pass an Optional
object as method parameter as we can't guarantee that the Optional
object itself is NOT null, you might still end up with a null
check. I suggest you can look here on a detailed discussion on this subject.
The other point is that Java's Optional
is NOT Serializable
, so you can't use Optional
on the variables/fields whichever require Serialization. You can refer more on this here.
So, the important point is that we still need null
references wherever Optional
does not make sense.