Add to Google Reader or Homepage

Java Object Serialization

What is meant by Object Serialization?


In General, Object Serialization refers to writing an object into a stream,you can consider a stream as a data structure used to store values. Mostly the stream used for object serialization is a FILE. The reverse process is called object deserialization i.e, reading the object from the stream.

Object serialization is useful when you want to store objects into a file like primitive data types are stored.



What do they actually meant by writing(storing) objects?

 Are we really writing the objects in to  a stream? The answer is certainly "no". Then what we are writing into a stream?.Actually we are writing the state of the objects,to put it simply 
"we are storing the non-static and non-transient fields of the class in to an external file".


Non-static field:


You could have known that a Non-Static field is a field(or variable) which is not preceded by the static modifier.

For example,  int a=10; 

You could have asked why a static field is not serialized by default?

The  reason is " If there is a non-static field in your class,then each object that you would create will have their won copy of that field but on the other hand if there is a static field then it will be shared by each object". So rather than serializing a static field for each object they(Sun Microsystems) could have left that.


Non-transient field:


Similar to Non-Static field, here the field is not preceded by the  transient keyword. What is its use? There are certain fields which cannot be serialized in java,and if you try to do so then Not Serializable Exception will be thrown.  So, in order to avoid this exception you have to use this keyword.

 Now  let's see how to serialize  and deserialize the object. If you want to serialize an object you need to use the ObjectOutputStream class of the java.io package as follows,

ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("primit.dat"));
os.writeObject(this);// Here "this" specifies the object to be serialized.
 
 Similarly for deserializing the object you need to use the ObjetInputStream class of the java.io package as follows,

ObjectInputStream is=new ObjectInputStream(new FileInputStream("primit.dat"));
SerializeDemo s=(SerializeDemo)is.readObject();
 
 
  what happens when you read object from the stream?

When reading(or Deserializing) the object,"we are not actually reading the object, a new object is re-constructed from the stream with the field values stored in it". Here i would like to highlight one thing, you all know that to construct an object we need the class definition and this applies even if you reconstruct the object i.e, when reading the object from the stream we need the actual class definition in the class path.

 

Program:

  
SerializeMain.java

class Serializemain
{
public static void main(String args[])
{
SerializeDemo p=new SerializeDemo();
p.execute();
}
}


SerializeDemo.java
 

import java.io.*;
import java.util.*;
import java.lang.*;
class SerializeDemo implements Serializable
{
String name;
int Mark;
int rollno;
public void execute()
{
try
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the name:");
name=in.next();
System.out.println("Enter the marks");
Mark=in.nextInt();
System.out.println("Enter the rollno");
rollno=in.nextInt();
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("primit.dat"));
os.writeObject(this);// Serializing the object
ObjectInputStream is=new ObjectInputStream(new FileInputStream("primit.dat"));
SerializeDemo s=(SerializeDemo)is.readObject();// Deserializing the object
System.out.println("Name:"+s.name);
System.out.println("Mark:"+s.Mark);
System.out.println("RollNo:"+s.rollno);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


 Output:

Enter the name:
ganesh
Enter the marks
80
Enter the rollno
34
Name:ganesh
Mark:80
RollNo:34

 
 
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top