• 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

About Serialization

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch , Sujeet Kumar Jaiswal
 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic