Win a copy of Hello World this week in the Python 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
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

ObjectInputStream question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java SE6 API, there is a paragraph in ObjectInputStream:

Serializable classes that require special handling during the serialization and deserialization process should implement the following methods:

private void writeObject(java.io.ObjectOutputStream stream)
throws IOException;
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException;
private void readObjectNoData()
throws ObjectStreamException;



Now that the method are all marked "private", how can they be "implement"ed? Can anyone give an example or shed some light on it? TIA
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the problem with implementing a private method. They are just giving you the liberty of using any access modifier you want. This is to ensure that no one calls the method on your class. These methods are usually meant for the JVM to use while serializing and deserializing...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these private methods are not declared in Serializable interface, Serializable is just an marker inteface, it is just a design that will tell you to write two private methods with given signature for custom serialization.
I am providing an example, see DogT class is performing custom serialization process and adding Collar information in serialization process.



 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Punit, your example helps a lot! I was wondering why I cann't find these methods in the ObjectInputStream API, and didn't realize that the programmer had to write their own implementation. Maybe I didn't read it carefully. This is some sort of stipulation the JVM made to help serialize/deserialize some objects that do not implement Serializable interface right?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jia Tan wrote:Thank you Punit, your example helps a lot! I was wondering why I cann't find these methods in the ObjectInputStream API, and didn't realize that the programmer had to write their own implementation. Maybe I didn't read it carefully. This is some sort of stipulation the JVM made to help serialize/deserialize some objects that do not implement Serializable interface right?



Right.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!