aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization help" Watch "Serialization help" New topic
Author

Serialization help

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
from k&b book...chapter 6, self-test answer



i cant figure about answer F, what does it do to "deserialize" and how does it help to alter the standard deserialization process by overriding the readObject() method in SpecialSerial??
Roland Schneider
Greenhorn

Joined: Sep 19, 2007
Posts: 12
No readObject(...) Method can be overriden, the method must be implemented in any class which needs special treatment in the deserialization process.

See Serializable Interface
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
Yes, I'm wondering the same thing. An example of what is meant by "F" would be extremely helpful. Answer "F" states: "In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial." My question is - if you wanted to alter the deserialization process, HOW do you implement the readObject() method in SpecialSerial? I tried putting different code lines from the main method into the SpecialSerial class, but with no success. All of my attempts resulted in compilation errors. An example would be very appreciated!
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Also its written static are desializable then y static variable is 10? Is it because static variable cannot have default as 0 for int?


http://www.lifesbizzare.blogspot.com || OCJP:81%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Katherine Reut wrote:Yes, I'm wondering the same thing. An example of what is meant by "F" would be extremely helpful. Answer "F" states: "In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial." My question is - if you wanted to alter the deserialization process, HOW do you implement the readObject() method in SpecialSerial? I tried putting different code lines from the main method into the SpecialSerial class, but with no success. All of my attempts resulted in compilation errors. An example would be very appreciated!



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Vishal Hegde wrote:Also its written static are desializable then y static variable is 10? Is it because static variable cannot have default as 0 for int?


The static variable "z" has a value of 10 after deserialization, because it had a value of 10 before deserialization. By default, static variables are not serialized or deserialized, so by default, it should not change.

And like instance variables, static variables are also initialized to default values, so the static int "z" should have a value of zero, sometime during when the class was first loaded (and initialized) into the JVM by the class loader.

Henry
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
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.
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
I'm going to answer my own question about Chapter 6 question 8 ...or rather put the answer that my coworker Lori's gave to me here:

This is how you implement the readObject() method in SpecialSerial in order to alter the standard deserialization process:



With these changes in SpecialSerial, the output is now:

10 7 9

Thanks again, Lori!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization help