• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ranchers,I have few doubts in serialization.

1)In kathy sierra and Bert Bates page462
"Java Serialization takes care of saving that object's entire "object graph".That means a deep copy of everything the saved objects needs to be restored.For example if you serialize a Dog object, the Collar will be serialized automatically."
But below this statement he gives an example,which throws a "NotSerializableException:Collar".
If sentence in the quotation is true then why is that we get this exception?


2)kathy sierra and BertBates page 469 last para
1)" When an instance of a serializable class is deserialized, the constructor does not run,and instance variables are Not Given their initially assigned values"
next in page 471 second para he states
2)"if you are a serializable class,but your superclass is not Serializable,then any instance variables you inherit from that superclass will be reset to the values they are given during the original construction of the object.This is because the non-serializable class constructor will run."

In the first statement it states that the constructor does not run,while in the second it states the constructor will run..?I am totally confused?


Thanks
Vinay
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You get the exception because Collar is not serializable. Obviously, in order to properly save the object graph for an instance you must have that all classes involved are serializable.

2) When you deserialize an object its constructor does not run. But if its superclass is not serializable, then the constructor for the superclass runs. These statements are not contradictory. We are talking about two different classes and two different constructors.

I hope that clarifies things.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Java Serialization takes care of saving that object's entire "object graph"



This means the object we are trying to serialize can have reference variables pointing to more objects.And all these objects will be serialized.Like if we serialize a Dog object, the Collar will be serialized automatically.
But this will only happen if all the objects have Serializable interface implemented to their classes.If this is not the case then NotSerializableException will be thrown.
NotSerializableException is occurring in the example where Collar class is not implementing the Serializable interface.



See in above code class B implements Serializable interface but its parent class A is not , so non-serializable class A constructor will run during deserialization.

I hope it helps

 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
k..now i got it.
thank you Sunny & Ruben.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic