aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
Source : Whiz Lab


When deserailzing there is a runtime error saying missing default constructor...
So does that mean that every time an object is created then the default constuctor is invoked irrespective of how the actual object is created(one argument)??
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The problem is that when an object of a class which is serializable (but which has a superclass which is not serializable) is deserialized, then the instance initialization code for the superclass (including a call to a no-args constructor) will run. In your case, since B is not serializable, when the object of type A is deserialized, the JVM will attempt to call the B() constructor, but since you haven't coded it (and because you have coded another constructor) the class doesn't have one, which is what is giving you the issue.


All code in my posts, unless a source is explicitly mentioned, is my own.
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
But the object has used the one argument constructor when it is actual created... So why it is not using the one argument constructor again???
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You are deserializing an object of class A, which you constructed using the constructor that takes 1 arg. The constructor for A is not run when you deserialize it, because that's the point of serialization. You just copy the state of object A from the serialization file. The problem is that not all of the object's state got serialized. Since A extends B and B is not serializable, when you serialized the instance of A, the part of the object state that is inherited from class B did not get serialized. The way Java fixes that is by running the default instance initialization code for objects of class B, and that includes a call to the no-arg constructor B(). But because B doesn't have that constructor, you get a runtime exception.

This is explained very well in the K&B book, do you have it?
raghuram vadlamani
Greenhorn

Joined: Feb 17, 2009
Posts: 5
what Ruben said is correct but you are creating object of the serializable class from which you are invoking the overloaded superclass constructor with super(x) but during deserialization it will get the serialized object but this one depends on the superclass "JVM will invoke Superclass constructer i.e no args one "
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
Thanks for clearing the doubt
Ruben Soto wrote:This is explained very well in the K&B book, do you have it?

May be i overlooked it...
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
Created a new topic.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Kedar Nath wrote: i more doubt...
What is the difference between JVM thrown exception and programmatically thrown exception...
the difference is kind of blur...
LIke the IllegalArgumentException(Programmatically), which is thrown when the method receives arg differently formated.But the method is actual receiveing the argument which is sent by the us!!
Similarly, ClassCastException(JVM), when coding we did the casting in a wrong manner.So it should be programmtically thrown..

But StackOverflowError, sound correct i mean as a JVM thrown..

Please dont if i sound stupid ...
Please explain

Kedar, it's better if you start another topic for this question, since it is a new question and that way more people will be able to answer you.
 
wood burning stoves
 
subject: Serialization