• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable interface

 
palas das
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we need to implement Serializable interface by a class??
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ask questions at this level in the Java In General (Beginners) forum.
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.

Cheers,
JD
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to make sure that all of your member variables are also Serializable or use the "transient" keyword to indicate they can be skipped when serializing. You'll get a NotSerializableException if there is a non-serializable object anywhere in your object graph - your member variables and their member variables and so on.

You can extend a non-serializable class and make it serializable by doing custom serialization in writeObject and readObject methods.

Whew. Does it still sound worth doing?
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to serialize a Singleton object ( google for Singleton pattern ) you must provide a readResolve method or else a new object is created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic