This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Serialization" Watch "Regarding Serialization" New topic
Author

Regarding Serialization

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
i am having some doubts in this program

1)how does line 1 behave?

2)what is the meaning of this line "In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial

import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("myFile"));
os.writeObject(s); os.close();
System.out.print(++s.z + " ");
ObjectInputStream is = new ObjectInputStream(
new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
} catch (Exception x) {System.out.println("exc"); }
}
}
class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
}

please explain in detail thanks in advance
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Since object serialization/deserialization are removed from OCJP 6.0 object, I did not review it for months.
Trust us. Many Java Ranchers who finished the exam told us that this topic was not tested.

But it is ok to learn more about it just in case. Some test centers may still use legacy test.

In your code, which line is label line 1? I did not see it.

Regarding to this : In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial

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.
Hope this help.

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

kiruthigha rajan wrote:1)how does line 1 behave?

Please UseCodeTags. Since your code does not contain line numbers, its difficult to answer this. Are you asking about this line? :
What exactly haven't you understood about this line?
Helen Ma wrote:I think that refers to a transient field, which is set to null during serialization process.

No. It's not set to null, indeed, serialized object does not contain that field itself.
Helen Ma wrote:You can customize the serialization / deserialization processes , so that value of the transient field can be serialized and even deserialized later on.

Well, yes, those methods are used to customize serialization/deserialization process, but serializing transient member should not be part of that customization. I don't remember if Siearra & Bates have used such example, but if yes, then its not a pleasant thing to do. If you anyway want to serialize a member, what's the use of keeping it transient?

The customization comes in handy with members which are not serializable e.g. you want class Employee to be serializable and Employee has-a Salay and class Salaray is not serializable, then you can do those things in those methods.

I hope it helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
1)how does line 1 behave?

System.out.print(++s.z + " ");//1

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

kiruthigha rajan wrote:1)how does line 1 behave?

System.out.print(++s.z + " ");//1


Did you try with this? What result did you get? What part did you not understand?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Serialization