This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization JSCP 6.0 (manually invoking writeObject() )

 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,

i have been reading this scjp 6.0 book, there is some explanation about how to serialize an object having relationship with another class's object which is not serializable ... below is the code, when i execute it i'm getting two exception (Java.io.IOException: readError & java.io.IOException writeError) the main aim of this code is to test serialization not proper code writing or following any standards.



regards,

Rafi (planning to appear for scjp 6.0 soon)
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization is not a part of SCJP 6 any more, so you don't have to worry about it now. Also please indent your code in the future. Now you can't close the input and output stream in the readObject and writeObject methods respectively. So remove the oos.close and ois.close method calls. Also in the readObject method you didn't assign the Collar object to theCollar field of the class...
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Serialization is not a part of SCJP 6 any more, so you don't have to worry about it now. Also please indent your code in the future. Now you can't close the input and output stream in the readObject and writeObject methods respectively. So remove the oos.close and ois.close method calls. Also in the readObject method you didn't assign the Collar object to theCollar field of the class...


oh sorry for that collar object .... the main problem is with writeObject method .....

as well how can i get the updated objectives for scjp 6.0 (i'm reading scjp 6.0 book by bert bates & sierra, according to the book serialization is part of the exam scjp 6.0 ) please let me know where i can access the exact objectives as i'm appearing for the exam soon.

regards,

Rafi
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Serialization is not a part of SCJP 6 any more, so you don't have to worry about it now. Also please indent your code in the future. Now you can't close the input and output stream in the readObject and writeObject methods respectively. So remove the oos.close and ois.close method calls. Also in the readObject method you didn't assign the Collar object to theCollar field of the class...


wait, its working now.

i have removed the ObjectOutputStream.close() & ObjectInputStream.close() from both the methods and the main class and it started working ??? isn't it weired.

i mean i should get a warning for using close() method, why was i getting exceptions???


hey Ankit any personal advise for the exam (i mean looking at your score, 98%, wow, you should be expert ....) give me some useful advises (i'm weak in assignments chapter).

regards,

Rafi
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rafi Fareen wrote:how can i get the updated objectives for scjp 6.0

The updated objectives are available here.
i have removed the ObjectOutputStream.close() & ObjectInputStream.close() from both the methods and the main class and it started working ?

Isn't that what I said??
i should get a warning for using close() method, why was i getting exceptions?

What type of warning?? The call is perfectly legal java code, so you won't get any warning. The writeObject and readObject methods we create are called by the serialization mechanism to give us control over the process, but after our code is done executing, there is some additional processing that is needed to serialize (or deserialize) the object. That's why you can't close the input/output stream. If you see the source code of ObjectOutputStream, in the writeSerialData method, there is a line of code
this line invokes our writeObject method, and after that some additional work is done by the ObjectOutputStream class, that's why closing the stream results in an exception...
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of warning?? The call is perfectly legal java code, so you won't get any warning. The writeObject and readObject methods we create are called by the serialization mechanism to give us control over the process, but after our code is done executing, there is some additional processing that is needed to serialize (or deserialize) the object. That's why you can't close the input/output stream. If you see the source code of ObjectOutputStream, in the writeSerialData method, there is a line of code
this line invokes our writeObject method, and after that some additional work is done by the ObjectOutputStream class, that's why closing the stream results in an exception...


looks like i understood your point exactly .... thanks bro
 
Muthi ur Rahman Mohammed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am Muthi. I was learning Serialization today and during the process came across this forum. I follwed the program and code. I have one question regaring the program. In the program collar size is set to 6 and dog size is set to 7 and serialization is done following the creation of dog object. The programmer is then changing the values of collar and dog sizes and follwing which deserialization is done. When output is printed, value of collar zize is 99 and dog size is 23 but the serialized object values were 6 and 7. How is this happening?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic