Course contentsShow
← Java
Lesson 2133 of 2,87042. SerializationFree lesson

Default Values After Deserialization

How transient fields are initialized to default values when an object is deserialized.

Default Values After Deserialization

What you'll learn: How Java initializes transient fields when reconstructing objects from serialized data.

When you deserialize an object, Java doesn't run its constructors—it rebuilds the object directly from the serialized byte stream. But what happens to fields you marked as transient? Since they were explicitly excluded from serialization, they won't be in that byte stream at all.

The Default Value Rule

When an object is deserialized, any transient fields are automatically initialized to their default values, exactly as if you had declared a field without initializing it:

  • Numeric primitives (int, double, etc.) → 0 or 0.0
  • boolean → false
  • Object references → null

Think of deserialization as Java creating a "blank slate" object and then filling in only the fields that were saved. Transient fields remain blank—they get Java's standard default values, not any custom initial values you might have set in your class definition or constructor.

Why This Matters

If your transient field was a cache, counter, or temporary computation result, starting at zero or null might be perfectly fine. But if your application logic assumes that field has a meaningful value, you'll need to handle re-initialization yourself (which you'll learn techniques for later).

For example, if you had transient int sessionCount = 100; in your class, after deserialization, sessionCount will be 0, not 100. The assignment in your class declaration doesn't execute during deserialization.

Key Takeaway: Transient fields are initialized to Java's default values (0, false, or null) during deserialization, regardless of any initial values declared in your class—constructors don't run when deserializing.