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,
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,
Comments are closed for "Object Construction in Serialization".
Such an useful post. The concept is very clear by the examples and by the explanation which is very simple to understand. Thank You.
Thank you Joe. The explanation is great. Now I am gonna try to read all your posts.
Very nice article. Clear our serialization concept.
Good article
Really Nice Information..
Thank you very much for sharing it.. :)
really a great post…thank you javaguy
Very nice article…Thank you for sharing
good article
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.
Nice ..article with images.Thanks..
Very crisp and clear…
can you please post some article on Annotations.
Great and interesting explanations!!!
Simple explanation of a very simply presented concept.
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
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
Simply superb!!!
thanks
Good article
Nice article
“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
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.
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 ?
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.
are all the functions in java implicitly virtual????
Nice Explanation Joe.
Thanks
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.
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?
:-(
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.
Sir You Explanation is good enough to know the process.
With an Example keeps like this comments Away.
[…] 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 […]
Why is Animal is the first non-serializable class in the 2nd e.g.
Why is Animal the first non-serializable class in the 2nd e.g.
Got the answer :)
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?
Just Awesome explanation.
If the vehicle class dont have a constructor it throws java.io.InvalidClassException: no valid constructor.
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.
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