Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Serialization JSCP 6.0 (manually invoking writeObject() )

 
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)
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
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?
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!