• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

understand readObjectNoData callback - Serializable

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.

I'm studying the java IO and faced the callback readObjectNoData at java API about Serializable interface.
I understood the use of readObject and writeObject callbacks, but this readObjectNoData is not clear for me.
So I'd like to see real example of using it. I've tried to create a Class "Monkey" that extends "Animal" (and Animal implements Serializable), and I've serialized it using FileOutputStream and ObjectOutputStream. So I changed the Monkey hierarchy making this class to extend Mammal instead. So, I thought it will be a good scenario to test the use of readObjectNoData, but I wrote a readObjectNoData within Monkey class to configure Mammal attributes manually. So my readObjectNoData aren't called when deserializing this object.

Could someone help me to understood better about readObjectNoData, and if possible show some example?

Tks!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adolfo Eloy wrote:
I understood the use of readObject and writeObject callbacks, but this readObjectNoData is not clear for me.
I've tried to create a Class "Monkey" that extends "Animal" (and Animal implements Serializable). I changed the Monkey hierarchy making this class to extend Mammal instead. So, I thought it will be a good scenario to test the use of readObjectNoData, but I wrote a readObjectNoData within Monkey class to configure Mammal attributes manually. So my readObjectNoData aren't called when deserializing this object.



Never actually used this method, as I have never had the need to protect against corrupted / changing class hierarchies.

But... I always thought readObjectNoData() was similar to the readObject() method, but for the case where the data isn't available. Given that, in your example, shouldn't it be the Mammal class that has the method, to handle the case of configuring itself, when it is not in the serialized data?

Henry
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Given that, in your example, shouldn't it be the Mammal class that has the method, to handle the case of configuring itself, when it is not in the serialized data?



Thanks Henry Wong! It's exactly as you said. I was studying it today testing all possibilities and reading more about it.
I'd like to add some more information from my studying bellow only to document more about, and if I'm wrong, correct me if possible.

Considering an object Monkey that extends only Object java class and that it's serialized as a file in disk with monkey.dat filename. So consider that another day, this Monkey starts extending Animal (that also implements Serializable interface) and we try to restore this file (monkey.dat). ObjectInputStream will try to recover all data that belongs to the actual class hierarchy, but there is no Animal data within this file. So, in this case readObjectNoData will be invoked, Animal is the best place to implement readObjectNoData because there is no better one that knows how to be restored than Animal itself.

Thank you.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic