Serialization is the process of writing an class object that is transmitted through the network in the encoded stream of byte form. This Encoded form can be reconstructed from the network by reading from byte of stream. This Process is called Deserialization.Serialization allow to save the object in file and transmitting the object through network. It does not write the class variable, because these variable are not a part of object state. It allow the object to be serialized ,without writing the method of object.
1)Serialization is used in various Java Technologies such as Enterprise java bean directly used with activate and passivate server.
2)The Technologies supports the invoking the method of an object on another host such as RMI AND CORBA used to transmit their parameter across the network
The Serializable class derived from java .io.serializable that defines no message. class implements serialization or extending class that also implements serialization define that class as one of the participants in serialization. The instance is used as parameter of ObjectOutputStream.writeObject and the result of ObjectInputStream.readObject.
When Exception or not exception is Thrown in Serialization
An object is encountered that is not serialized like your collection element, these
method throws not serialized exception. Incase, if an object contains variables
of class type that refer to objects whose classes are not serializable, the
object stream methods will
attempting to write or read an instance. Even ,when a collection is
serializable but contains objects that are not serializable, an exception will
If that field refers to an instance of a serializable class, no exception occurs
upon serialization. The library classes such as serializable that includes
wrapper , collection classes, GUI component classes,
are some library classes that are not serializable include
reflection classes (
Method, etc.), stream classes,
Graphics , and
Image. If that field refers to an
instance of a serializable class, no exception occurs upon serialization.
The Serializable interface in java are java.io package that implements the object serialization in java.
1) The Serializable interface-This interface is used for serializing objects in java and does not have any method defined inside it.If a class is serializable all of its subclasses are also serialized.
Syntax used to implement serializable interface
class xyz(class_name) implements serializable
2) The ObjectOutputStream interface- This class extends the Output Stream class and implements its interface. This class writes an object of the primitive data type and data object to the Output stream.
List of the method in ObjectOutputStream:
|Public void writeObject(Object obj)throws IO Exception||When you Write an obj Object to the ObjectOutputstream stream|
|public void flush( ) throws IO Exception||This method allows the data available in a buffer is written to a file to which stream is connected.|
3)The ObjectInputStream interface-This class extends the Input Stream class and its interface of Object Input .The ObjectInputStream deserializes the data and object written using the ObjectOutputStream.
List of method in ObjectInputStream :
|public final Object read Object( )throws IO Exception,ClassNotFoundException||This method is used to reads an object from ObjectInputStream|
|public string readLine( )throws IO Exception||This method is used to read a line that has been terminated by End of File|
In this code we try to describe a class name Student that implements Serializable interface used to serialize its objects. We declare a constructor inside the class that accept the parameter and void ( ) method is used to display the parameter passed in the constructor. But on Runtime the command prompt show no method such found exception.
public class Student implements Serializable
private String stud_name;
private int stud_rollno;
private String stud_address;
public Student ( )
Output On Command Prompt
Exception in thread "main" java.lang.NoSuchMethodError: main
In order to overcome exception, you need to serialize an object of the Student class using write Object( ) method of the ObjectOutputStream class.
|This method write the object to the Output Object Stream i.e student.txt.|
|This ensure the data stored in buffer is written to a file to which stream is connected.|
|This is used to release the resources occupied by stream.|
| public class WriteStudent
public static void main(String args)
Student s=new Student("Saurabh",1,"New Delhi");
FileOutputStream fos=new FileOutputStream("student.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
In the above code an object of the student class is serialized into a file name student.txt.The above command prompt does not display any output on the screen, rather it create a file student.txt.
Output on Command Prompt
C:\Documents and Settings\Administrator>cd\
The readobject method of the ObjectInputStream class is used to read the serialized object from the student.txt.
readObject( ) -is used to read the serialized object from the student.txt.
The object returned by the readobject( ) is typecast into required class.
| public class ReadStudent
public static void main(String args)
Student s=new Student();
FileInputStream fis=new FileInputStream("Student.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
System.out.println("Excxeption:"+ioe.toString( ) );
System.out.println("Exception:"+cnfe.toString( ) );
Output on the Command prompt
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: November 10, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles