This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understand readObjectNoData callback - Serializable

 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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!!
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic