|
Re: Problem with read and write Collection
|
Posted: Jan 2, 2004 6:22 AM
|
|
What is the output of this code? Does it throw any exception (like NullPointerException or ClassCastException)? Does it print the objects, just that they aren't the same?
When loading serialized objects, the comparison a==b doesn't work, because they aren't the same object. Serialization doesn't maintain object identity, just load its contents back, keep this in mind.
Because of that, unless you override tne device.toString() method to print its fields' values, it will print different strings (the Object class default implementation returns somewhat like a pointer).
If the implementation of Device contains any transient field, or any of its field's objects contains it, it will not be serialized, and when you load it, they will be set to null.
And, make Device implement the interface Serializable . It doesn't contain any abstract method, so you don't need to add any code besides implements Serializable , but it marks the class as serializable.
What is p.readObject(); intended to do? Well, it doesn't matter, anyway, but the code you posted won't compile, because p is not declared, unless it is a object field (p as a field name? BAAAD practice!) =)
And, just a remark, you aren't serializing a Collection, but just an array of Device s (you coded it right, anyway)
|
|