Object Construction in Serialization

11/12/2011

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.

  • RULE1: When you serialize an object, the whole inheritance hierarchy is serialized till the first non-serializable class of that object.

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.

  • RULE2: The first non-serializable class must have 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.

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

Giftvincy on December 12th, 2011 6:48 am

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

mesut on December 12th, 2011 7:16 am

Very nice article. Clear our serialization concept.

Rajeev on December 12th, 2011 12:59 pm

Good article

Jasdeep Singh on December 12th, 2011 1:27 pm

really a great post…thank you javaguy

Vinod Gulia on December 15th, 2011 11:59 am

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

Chetan Pagar on December 15th, 2011 2:09 pm

Very nice article…Thank you for sharing

meen on December 28th, 2011 6:42 am

good article

Dhiraj on January 3rd, 2012 9:30 am

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.

Akhtar on January 4th, 2012 5:30 am

Nice ..article with images.Thanks..

JavabynataraJ on January 5th, 2012 12:46 pm

Very crisp and clear…

can you please post some article on Annotations.

Anonymous on January 7th, 2012 4:44 am

Great and interesting explanations!!!

Kat on January 9th, 2012 7:58 pm

Simple explanation of a very simply presented concept.

Jayant on January 17th, 2012 12:00 am

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

Raja on January 27th, 2012 6:10 pm

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

tarun on February 14th, 2012 9:41 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home