Henry Wong wrote:First, what have you tried? And second, why are you trying to "putting different code lines from the main method into the SpecialSerial class"? The syntax is very specific, you can just try "different code". Just search the ranch, there are lots of examples. And if you are not sure what to search, try starting with the "Similar Threads" section below on this page.
Henry
I don't understand what the authors meant by "implementing the defaultReadObject() method in SpecialSerial." I tried moving the line "SpecialSerial s2 = (SpecialSerial)is.readObject();" from the TestSer class to the last line in the SpecialSerial class. This resulted in a compilation error. I also tried moving the following four lines from the TestSer class to the last line in the SpecialSerial class.:
ObjectInputStream is = new ObjectInputStream(new fileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
This also resulted in compilation errors.
I also tried changing the SpecialSerial class to say
"class SpecialSerial implements Serializable, readObject() {"
but obviously that didn't work.
Thank you for directing me to the similar threads. The
thread that asked the same question that I did got the following answer:
"I think that refers to a transient field, which is set to null during serialization process. You can customize the serialization / deserialization processes , so that value of the transient field can be serialized and even deserialized later on. Refer to p467 of KB's book. There are two private method writeObject and readObject. They are doing customized serialization for the transient field of the Collar. The writeObject method write the Collar's values to file even though it is transient. The readObject can read it from the file. "
I found the Collar example understandable, but I can't figure out how to translate what was done in it to this problem. HOW do you implement the readObject() method in SpecialSerial? I'm sorry for being so confused.