I dont understand the effect of serialization. What can it actually contribute to the following objects and code?
i just cant see it!
i tried to
test the effect by altering the code but dont know where to start...coz i am not quite sure (still) what serialization is ALL about!
K&B added in order to alter the standard deserialization process you would override the readObject() method in SpecialSerial. Why???
and most importantly, if serialization is about saving objects (instance variables), line 9's ++s.z is outside os.close(),
how come on line 14, the value changed?
====here is the code======
import java.io.*;
public class TestSer {
public static void main(
String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
os.writeObject(s);
os.close();
System.out.println(++s.z + " "); //line 9
ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z); //line 14
} catch (Exception x)
{System.out.println("exc"); }
}
}
class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
void readObject(){ };
}
[ September 17, 2007: Message edited by: adam lui ]