Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private(?) writeObject/readObject

 
Roely de Vries
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question about writeObject and readObject,


In this code fragment b cannot be serialized,therefore it is marked as transient in A.

A is serialized an deserialized by TestSer. Because A has the private methods writeObject and readObject instead of os.defaultWriteObject and ois.defaultWriteObject.

The question is, how is this possible?
writeObject and readObject are private methods of A, so how can they be invoked by the serialization proces?


(edited: added tags)
[ January 30, 2007: Message edited by: Barry Gaunt ]
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, how is this possible?
writeObject and readObject are private methods of A, so how can they be invoked by the serialization proces?


Other questions that you may also ask include:

- How does the defaultReadObject() method of the stream object access the private instance variables?

- How come the readObject() method doesn't call the super.readObject() method? Don't the parent class have special serialization needs too?

- How come all readObject() and writeObject() methods are private?

Serialization is one area where the "basic rules of Java" does not apply. You need to learn it independently -- without thinking in java.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic