Shouldn't Optional be Serializable?

Mike Duigou mike.duigou at
Fri Sep 27 17:15:22 PDT 2013

On Sep 17 2013, at 16:32 , Vitaly Davidovich wrote:

> Presumably because you may want to have class fields that express
> nullability via Optional rather than null.

Using Optional as a field type doesn't seem to offer much.

Would the Optional field be wrapped in accessors? 

If it's not wrapped, the result is that orElse(default) gets spread across the usage of the field which is a poor way to apply a default.

If if it is wrapped, why not apply the default in the accessor or in the setter and not bother with Optional?

>  Whether that's a good design or
> not is a separate question;

That's not generally how JDK expert groups think. Adding any feature to intentionally support bad design wouldn't make much sense.  Understanding the dimensions of how a feature might be used is certainly something that the EG spends a lot of time on. The choice of semantics, methods offered and interfaces implemented isn't ever separated from the expected use cases. Optional is a new class/feature to the JDK and it seemed appropriate to start with a minimal implementation.  Concern that Optional would be misused in other use cases threatened to derail it's inclusion in Java entirely!  Optional is being added for the value it offers in "fluent" sequences of statements.  In this context use of Optional as a visible type or for serialization isn't relevant.

> conceptually, I don't see a reason why Optional
> cannot support that.  For "reference", Google Guava's version is
> serializable.

Guava's Optional certainly proves that it is technically possible (which was probably already known anyway) but that doesn't address whether doing so with the Java Optional would be a good idea or not.

>  If someone were to replace their use with jdk's Optional
> then they will hit exceptions if the owner class is serialized.

While the incompatibility is certainly unfortunate this is probably not a sufficient reason by itself to make Java's Optional serializable.


> Sent from my phone
> On Sep 17, 2013 6:06 PM, "Remi Forax" <forax at> wrote:
>> On 09/17/2013 11:44 PM, Pete Poulos wrote:
>>> Shouldn't java.util.Optional be Serializable?  Is there a good reason for
>>> it not be?
>> wrong question.
>> the right one is why do you want Optional to be Serializable.
>> Thanks,
>>> Pete
>> cheers,
>> Rémi

More information about the jdk8-dev mailing list