File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Serializable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serializable interface" Watch "Serializable interface" New topic

Serializable interface

palas das

Joined: May 09, 2007
Posts: 9
When we need to implement Serializable interface by a class??
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please ask questions at this level in the Java In General (Beginners) forum.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
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.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
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.

I agree. Here's the link:
subject: Serializable interface
It's not a secret anymore!