26 - Java Serialization using transient keyword - Theory

preview_player
Показать описание
@backstreetbrogrammer

--------------------------------------------------------------------------------
Chapter 12 - Serialization using transient keyword
--------------------------------------------------------------------------------
Suppose one of the objects in the “object graph” is not accessible (meaning we can’t modify it due to permission issues) and not marked as Serializable. In that case, are we blocked to serialize our main class which has reference to that read-only object?

One option is to subclass that read-only class, but it may be marked as final. OR, we don’t know if the read-only class object has other similar objects in its own “object graph”.

There are other private members fields (primitive and reference) of a class which contains sensitive data like passwords, or it doesn’t make sense to serialize it - like the state of an in-memory Thread.

That’s where the transient modifier comes in. By marking the members fields (primitive and reference) as transient, these fields are NOT serialized just like static fields.

What happens to data marked transient on deserialization? It reverts to its default Java values, such as 0.0D for double, false for boolean or null for an object.

#java #javadevelopers #javaprogramming #javaserialization
Рекомендации по теме