• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Serialization

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)how does line 1 behave?

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

 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic