File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes About Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Serialization" Watch "About Serialization" New topic
Author

About Serialization

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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.


Ping & DNS - my free Android networking tools app
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you asking for an explanation of your answer?
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
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

Joined: Dec 11, 2004
Posts: 13410

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
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

Joined: Oct 13, 2005
Posts: 38350
    
  23
Welcome to the Ranch , Sujeet Kumar Jaiswal
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Serialization