Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization !!!!!!!!!!!!! Help me out  RSS feed

 
Appulse Bond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody !
Can any body please help me to understand the serialization . I have some questions

1. What is need of writeObject and readObject in Serializable ?

2. If a class contains the other class(not implementating the serializable interface ) as attributes , can automatically Serialization process also serialized other class object or it will throw exception ?

3. What is advantage of Externalization over Serialization ?
 
Rishi Shankar Rengasamy
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

1. What is need of writeObject and readObject in Serializable ?
Reply -->

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:


private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

For Example
Check out the following code from LinkedList.java

/**
* Save the state of this <tt>LinkedList</tt> instance to a stream (that
* is, serialize it).
*
* @serialData The size of the list (the number of elements it
* contains) is emitted (int), followed by all of its
* elements (each an Object) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();

// Write out size
s.writeInt(size);

// Write out all elements in the proper order.
for (Entry e = header.next; e != header; e = e.next)
s.writeObject(e.element);
}

/**
* Reconstitute this <tt>LinkedList</tt> instance from a stream (that is
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();

// Read in size
int size = s.readInt();

// Initialize header
header = new Entry(null, null, null);
header.next = header.previous = header;

// Read in all elements in the proper order.
for (int i=0; i<size; i++)
add(s.readObject());
}


-------

2. If a class contains the other class(not implementating the serializable interface ) as attributes , can automatically Serialization process also serialized other class object or it will throw exception ?
Reply -->

It will throw an NotSerializableException.
When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

------


3. What is advantage of Externalization over Serialization ?
Reply -->
U can customize the serialization process.
The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state.

--------
 
Appulse Bond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanx for that !

Can we write the non serialized object , static object by the writeObject .

Moreover Externalizable disable native Serializable . What is the meaning this statement

Thanx in advance !!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a number of guidelines on HowToAskQuestionsOnJavaRanch.

These include:
EaseUp and UseRealWords. Please take the time to visit these links and follow the suggestions.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Appulse Appulse",
The display name rule is not optional.

We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name.

thanks,
Dave.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO and Streams...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!