This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About serialization

 
C Law
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand how the state of an object is preserved.

For one thing:

Animal a = new Animal();
try{
FileOutputStream fo = new FileOutputStream("test.ser");
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(a);
oo.close();
}
catch(IOException ex){
ex.printStackTrace();
}

What is test.ser? Is it an actual file? How can I found it?

Secondly, it seems that I can modify instance variable after
the object containing it has been serialized:

public class Animal{
String name = "Mutsang";

public static void main(String[] argv){
Animal a = new Animal();
try{
FileOutputStream fo = new FileOutputStream("test.ser");
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(a);
oo.close();
}
catch(IOException ex){
ex.printStackTrace();
}

a.name = "Tiger";

try{
FileInputStream fi = new FileInputStream("test.ser");
ObjectInputStream oo = new ObjectInputStream(fi);
oo.readObject();
System.out.println(a.name);
oo.close();
}
catch(IOException ex){
ex.printStackTrace();
}
//output is Tiger.

If the state is really saved in "test.ser", how come that when
the object is restored, its "name" field is changed?

Thanks in advance!
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: When you run this command...



What do you think happens to the object that is returned?

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic