Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Tuesday, 4 October 2016

Java Tutorial : Java IO (Java Serialization - Important points)

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

Java Serialization is a powerful mechanism used to convert an object's state into a byte stream for storage or network transmission. However, mastering it requires knowing the hidden nuances, subtle behaviors, and interview-critical rules that dictate how objects behave during the serialization and deserialization lifecycle.

Essential Points of Java Serialization

1. The Role of the Serializable Interface

Serializable belongs to java.io and is a marker interface. It contains no methods or variables, serving solely to inform the JVM that instances of your class are eligible for byte-stream conversion.

2. Static and Transient Variables

Serialization preserves only the state of an object, not class-level information:

  • static variables: Belong to the class, not individual object instances, and are never saved during serialization.
  • transient variables: Explicitly marked to be excluded from default serialization. Upon deserialization, they are restored to default type values (e.g., null, 0, or false).

3. Object Graph and References

When serializing an object that holds references to other objects, all referenced objects must also implement Serializable. If any referenced object in the graph is not serializable, the JVM throws a runtime NotSerializableException.

4. Constructor Execution During Deserialization

When deserializing an object whose class implements Serializable, no constructor of that serializable class is invoked. However, if a non-serializable superclass exists in the inheritance hierarchy, its no-argument constructor will be executed.

Key Takeaways for Developers

  • Always declare a serialVersionUID to safeguard against version mismatches and InvalidClassException.
  • Use transient for sensitive fields such as passwords or session tokens.
  • Ensure the complete object hierarchy is serializable when working with nested structures.

Related Tutorials from Ram N Java


Click the below Image to Enlarge
Java Tutorial : Java IO (Java Serialization - Important points) 
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java Serialization rules)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    Serialization in Java is governed by clear runtime rules that determine how an object's state is captured, stored, and restored. Overlooking these fundamental rules often results in unexpected exceptions such as NotSerializableException or InvalidClassException. In this tutorial, we break down the critical rules you must follow when working with Java IO serialization.

    Essential Rules of Java Serialization

    1. The Serializable Contract

    For any object to be serialized, its class must implement the java.io.Serializable marker interface. If you attempt to pass an object that does not implement this interface to ObjectOutputStream.writeObject(), the JVM throws a runtime NotSerializableException.

    2. Inheritance Rules

    • Superclass implements Serializable: If a parent class implements Serializable, all child subclasses automatically inherit serializability without needing to declare the interface explicitly.
    • Superclass does NOT implement Serializable: If the subclass implements Serializable but its parent does not, the non-serializable superclass must have an accessible no-argument constructor. During deserialization, that superclass constructor executes to initialize the inherited state.

    3. Handling Static and Transient Members

    Serialization captures only instance state:

    • static fields: Belong to the class definition rather than the object instance, so they are never saved into the byte stream.
    • transient fields: Specifically flagged to skip serialization. When reconstructed, they receive default initial values (such as null, 0, or false).

    4. Complete Object Graphs

    If a serializable class contains references to other custom objects (HAS-A relationship), every single referenced object must also be serializable. If even one referenced object lacks the Serializable interface, the entire serialization process fails.

    Quick Reference Summary

    Component Serialization Rule
    Serializable Interface Mandatory; otherwise throws NotSerializableException
    serialVersionUID Recommended to explicitly declare to prevent version mismatches
    Static / Transient Both are ignored during the serialization process
    Non-serializable Superclass Must provide an accessible no-arg constructor

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=H4ZkBxp06kU&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java Serialization rules) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Tutorials