Languages
[Edit]
EN

Java - why java.util.Optional is not serializable?

5 points
Created by:
isherwood
599

1. Solution for common problems with Optional serializable exception

Edit

JDK Optional doesn't implements Serializable interface. So how can we work with this Optional when dealing with serialization?

Let's take a look at this example:

Instead of using Optional as field like we see above, we can use Optional as return type from getter and create new instance on each getter invocation.

Check this link to see how to solve common java serialization problem with full code example that I described above:

this link

2. Guava serializable Optional

Edit

If we want to use serializable Optional, we can use Guava's Optional.

For contrast we can compare java.util optional internals:

3. Shouldn't Optional be Serializable?

Edit

Answer from Brian Goetz (JDK architect from Oracle)

The best parts from the emails:

Part 1

Part 2

Part 3

Full email list:

References

Edit
  1. Shouldn't Optional be Serializable? - Brian Goetz
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join