change in state of a serialized object while constructing it
hi,
here is sample code on serialization.
before and after serialization of the object, there is a change in state.
Can anybody explain it.
age Serialization;
import java.io.*;
public class CodeWalkSeven{
public static void main(String [] args) {
Car c = new Car("Nissan", 1500, "blue");
System.out.println("before: " + c.make + " "+ c.weight);
try {
FileOutputStream fs = new FileOutputStream("Car.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
os.close();
}catch (Exception e) { e.printStackTrace(); }
try {
FileInputStream fis = new FileInputStream("Car.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Car) ois.readObject();
ois.close();
}catch (Exception e) { e.printStackTrace(); }
System.out.println("after: " + c.make + " " + c.weight);
}
}
class NonLiving {}
class Vehicle extends NonLiving {
String make = "Lexus";
String color = "Brown";
}
class Car extends Vehicle implements Serializable {
protected int weight = 1000;
Car(String n, int w, String c) {
color = c;
make = n;
weight = w;
}
}
Output :
before: Nissan 1500
after: Lexus 1500
Regards,
- srihari

