Object Construction in Serialization

My previous article on exploring java serialization is a box office hit. In continuation to that and popular request, I am going to write on how instances are created during serialize and de-serialize process.

There is a mystery. If you strongly believe that the constructor of a class is called everytime when an object is instantiated, then you have got a surprise.

To understand the above, consider the two example scenarios given through diagram,

Serialization Inheritance Hierarchy Example 1


Serialization Inheritance Hierarchy Example 2

  1. Vehicle is the first non-serializable class.
  2. Animal is the first non-serializable class.

Therefore, when you serialize an object of HybridCycle or Lion, as a chain it will save the state till Vehicle and Animal classes respectively.

Animal class must have an empty constructor. In the case of Cycle inheritance chain, Vehicle is the last class. Vehicle implicitly extends Object and thus gets access to an empty constructor.

This rule makes sense when you de-serialize. When you de-serialize, the object is not constructed using it’s constructors. Objects are instantiated using the saved state when it was serialized. Deserialization stops at the first non-serializable class and the empy constructor will be used.

In de-serialization,

  1. For Vehicle scenario, Object’s empty constructor will be used.
  2. Animal must have an empty constructor. If you have a parameterized constructor in Animal, you must explicitly add the empty constructor or you will get compilation error.

This Core Java tutorial was added on 11/12/2011.

Comments on "Object Construction in Serialization" Tutorial:

  1. Giftvincy says:

    Such an useful post. The concept is very clear by the examples and by the explanation which is very simple to understand. Thank You.

  2. mesut says:

    Thank you Joe. The explanation is great. Now I am gonna try to read all your posts.

  3. Rajeev says:

    Very nice article. Clear our serialization concept.

  4. Jasdeep Singh says:

    Good article

  5. Chetan Pagar says:

    Really Nice Information..
    Thank you very much for sharing it.. :)

  6. Vinod Gulia says:

    really a great post…thank you javaguy

  7. meen says:

    Very nice article…Thank you for sharing

  8. Dhiraj says:

    good article

  9. Akhtar says:

    I joe, thank you for sharing such good post. But i have doubt in 2nd rule you specify must have default constructor is not clear to me.

  10. JavabynataraJ says:

    Nice ..article with images.Thanks..

  11. Anonymous says:

    Very crisp and clear…

    can you please post some article on Annotations.

  12. Kat says:

    Great and interesting explanations!!!

  13. Jayant says:

    Simple explanation of a very simply presented concept.

  14. Raja says:

    This defenitions and post and example was very very good and interesting to read and easily understable, please keep post like this.

    Thanks a lot Joe

  15. tarun says:

    Hi Joe,

    Thanks for the sharing good java material. I have gone through lot of articles on this site and they are quite useful. I slight disagree with the information provided on this page. I don’t think the rules apply for serialization as such. These rules are related to inheritance not serialization

  16. Shazz says:

    Simply superb!!!
    thanks

  17. Madhav says:

    Good article

  18. Satish says:

    Nice article

  19. Pradeep Kesarwani says:

    “If you have a parameterized constructor in Animal, you must explicitly add the empty constructor or you will get compilation error”
    Just to correct it – We will get the Runtime Exception : java.io.InvalidClassException: … no valid constructor

  20. chandan says:

    hello sir ,
    I have a question.
    class A {
    int a ;
    }
    class B extends A implements serializable
    {
    int b ;
    String name;

    }
    Q> what are the properties are going to be serialized.

  21. Naren S says:

    Hi Joe, Thanks for the article. But you have said, just before the rule 2,

    “Animal class must have an empty constructor. In the case of Cycle inheritance chain, Vehicle is the last class. Vehicle implicitly extends Object and thus gets access to an empty constructor”

    Even, the Animal class would have access to the Object class right ? Because, Every class will implicitly extend the Object class right ?

  22. Mohit says:

    I have one question.

    Why serializale is marker interface. Why not readObject and writeObject signatures added to it. If these two methods were added to it then it become very clear for any one using serialized his class.

  23. twinkle says:

    are all the functions in java implicitly virtual????

  24. Venkatesh says:

    Nice Explanation Joe.
    Thanks

  25. Krishna says:

    Y did u beat the bush by rounding rounding …. rather hitting the correct point….!!????

    U titled it as “Object construction in serialization” but where did u do that !????

    U told it restores the saved state … !!

    But how does it do !????? Tell us that instead of writing pages and pages …!!!

    If you dont know that …. its not a problem .. !!

    But nice attempt !!! Nice day.

  26. Joe says:

    This article is about how the object is constructed while serializing and de-serializing it and the role of constructors.

    Didn’t I explain it in simple terms?
    :-(

  27. deepak says:

    I have a doubt about Rule 1
    “When you serialize an object, the whole inheritance hierarchy is serialized till the first non-serializable class of that object.”

    As per the rule first non-serializable is also serialized. If that is the case then why the constructor of first non-serializable class is invoked.

  28. Mani says:

    Sir You Explanation is good enough to know the process.
    With an Example keeps like this comments Away.

  29. […] have seen enough about using the default protocol to implement serialization and how instances are created during serialization. In this current article we shall see about modifying the default […]

  30. Rahul says:

    Why is Animal is the first non-serializable class in the 2nd e.g.

  31. Rahul says:

    Why is Animal the first non-serializable class in the 2nd e.g.

  32. Rahul says:

    Got the answer :)

  33. Devendra says:

    Hi JOE,
    Could not understand the below line…

    “1.For Vehicle scenario, Object’s empty constructor will be used.”

    Why Object’s Constructor will be called? Here super class for bicycle is vehicle.

    Can you please explain?

  34. Anonymous says:

    Just Awesome explanation.

  35. Prashant says:

    If the vehicle class dont have a constructor it throws java.io.InvalidClassException: no valid constructor.

  36. Mahesh says:

    Hi,
    Nice Article,
    1 .Can u please update as per comment for compile time error if empty constructor is not defined actually it is runtime.
    2. Every class have default objects empty constructor.

  37. Nagendra says:

    You explained very nicely using simple terms. Please do not bother about that kind of comment.

    Keep rocking.

    @Krishna: If you are a professional, please do not post these kind of comments. If you need any clarifications or more information then u should request for them not demand :P

Comments are closed for this "Object Construction in Serialization" tutorial.