Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Serialization in java

  Asked By: Pamela    Date: Jan 21    Category: Java    Views: 582
  

As we know Serialization in java has no methods then how it retains
the state . i.e. where does that logic lies where it helps us to
retain the state

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Adalia Fischer     Answered On: Jan 21

Serializable interface is just only used to tell that this class can
be serialized thats all... no logic  lies there, although you can
serialize your object by

File f0 = new File("d:/abc.obj");
FileOutputStream fos = new FileOutputStream(f0);
ObjectOutputStream os = new ObjectOutputStream(fos);
myObject mo = new myObject();
// do some thing with mo
//......
os.writeObject(mo);
os.flush();
os.close();
and you may deserialize the same object by

FileInputStream fis = new FileInputStream(f0);
ObjectInputStream ois = new ObjectInputStream(fis);
myObject mo2 = (myObject) ois.readObject();
ois.close();

the logic lies in the ObjectOutputStream and ObjectInputStream, you
may also add the variable
static final long serialVersionUID = -1564731489190550833L;
for maintaining the serialized versioning for your class.

 
Didn't find what you were looking for? Find more on Serialization in java Or get search suggestion and latest updates.




Tagged: