• Post Reply Bookmark Topic Watch Topic
  • New Topic

Externalizable vs Serializable with volatile fields  RSS feed

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I cant understand why we ever use externalization when the same thing can be achieved using custom serialization and declaring non serializable fields with volatile keyword
 
Arulkumaran Nedumaran
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the documentation, Externalizable interface says
"Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supercede customized implementations of writeObject and readObject methods."

In Serializable, we can let JVM take care of these things(unless we write the private write/readObject methods in which case we use out.defaultWrite/ReadObject() methods)

I think Serializable interface is a convenient way of leaving default stuff to Java while we concentrate on only the specific data parts of our serializable class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!