• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Serialization

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My interview asked what is serialization. I said " Serilazation is the process of storing the state of object by converting it in to bytestreams. when the object transferred into network that time you need serialization(used in RMI) .1) Implementing serializable interface and another way of 2) writeObject , readObject to do serialization. I need clear explanation please. i googled but not clear
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is a more general process than you describe; it needn't result in binary data. For example, if the objects in question adhere to JavaBeans conventions, then you can use the java.beans.XMLEncoder/XMLDecoder classes to obtain a serialized XML representation of the objects.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking for an explanation of your answer?
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s. serialization is use for storing the state of an object and for transporting to the object to remote computer. what is mean by storing the state of an object.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's Java's way of writing objects to disk.

Example:
When you stop or restart an application in the Tomcat application server, Tomcat will serialize all of the session objects and save them to disk.
When the app starts up again it reads them from disk and loads them back into memory. This allows you to restart the server or an application without interrupting people's sessions.

As Ulf mentioned: This won't occur unless all the objects bound to session implement Serializable and are serializable. Something like a socket or a database connection, for example can not be serialized to disk.
 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is a process of persisting an object's state. For serialization, it is necessary that the object's class or any class above it in the inheritance tree (superclass) must implement the marker interface "Serializable" (i.e. if a class is serializable, then all the class below in the inheritance tree will be automatically serializable). Also if the class has instance reference variable to any other class's object then that referred class must be also serializable otherwise it will give a Runtime Exception during serialization.

If you don't want to serialize any instance variable then declare it transient. Hence, it will be skipped from being serialized.

During deserialization, the object's state will be restored and all the instance variables will get their state back except the transient instance variables which will get its default value.

Note that during deserialization,
1- the constructor of the object's class is not called.
2- The constructor of any class above it in the inheritance tree is not called if they are serializable. For e.g.

Inheritance Tree:

A(Non Serializable)
|
B(Serializable)
|
C (Serializable)

So if C's object is deserialized then A's constructor will be called but not B's. Hence, A's inherited part in C will get different value as per the A's constructor but B's part will get the same value as persisted.

 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Sujeet Kumar Jaiswal
 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra Prakash wrote:what is mean by storing the state of an object.


An object has state (instance variables) and behavior (methods). When an object is constructed, its instance variables are assigned some value (i.e. the object's state is set) as per the constructor definition. And then during the lifetime of the object, its state may change (i.e. the instance variable's value may change).

Once we decide to serialize this object, it means that we want to persist the current state(instance variable's value) of the object.

@Campbell: Thanks for your warm welcome
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic