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

[1374 byte] By [Srihari.Ka] at [2007-11-27 4:41:07]
# 1
Because Vehicle is not serializable, its default constructor is called during deserialization, which initializes its fields to their default values, e.g. "Lexus".
ejpa at 2007-7-12 9:52:25 > top of Java-index,Core,Core APIs...
# 2
Thank you
Srihari.Ka at 2007-7-12 9:52:25 > top of Java-index,Core,Core APIs...