Java Reference
In-Depth Information
Listing8-16 ' s main() methodinstantiates SerEmployee withanemployeename.
This class's SerEmployee(String) constructor passes this argument to its Em-
ployee counterpart.
main() next calls Employee 's toString() method indirectly via Sys-
tem.out.println() , to obtain this name, which is then output.
Continuing, main() serializesthe SerEmployee instancetoan employee.dat
file via writeObject() . It then attempts to deserialize this object via readOb-
ject() , and this is where the trouble occurs as revealed by the following output:
John Doe
se object written to file
java.io.InvalidClassException: SerEmployee; SerEmployee;
no valid constructor
at
java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:730)
at
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
at
java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at
java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at
Serializa-
tionDemo.main(SerializationDemo.java:37)
Caused by: java.io.InvalidClassException: SerEmployee; no
valid constructor
at
java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:488)
at
java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:327)
at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1130)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at
Serializa-
tionDemo.main(SerializationDemo.java:27)
Search WWH ::




Custom Search