Rajat Gupta
Rajat Gupta

Reputation: 26587

How do I ensure that a java object is serializable?

I'm using a 3rd party library that asks to use only serializable object with its methods. How do I ensure that a java object is serializable ?

Sometimes my objects are just a list, a string or a simple POJO.

Upvotes: 1

Views: 1119

Answers (4)

Stephen C
Stephen C

Reputation: 718758

The answer is more complicated than the top-voted answer says. This is a summary of the requirements:

  1. Values of primitive types, String objects and null are all serializable.

  2. Arrays are serializable provided that the elements of the array are all serializable.

  3. Otherwise, the class of the object that you want to serialize must (directly or via inheritance) implement either the Serializable interface or the Externalizable interface. An enum class implements Serializable via its Enum superclass.

  4. If Serializable is implemented by the object's class, then the class must EITHER implement custom (typically private) readObject and writeObject methods, OR the values of all non-static, non-transient fields of the class must be serializable.

  5. If a Serializable class extends a superclass (other than Object) that doesn't also implement Serializable, the superclass needs to provide a no-args constructor.

  6. An Externalizable class should provide a no-args constructor and should implement readExternal and writeExternal.

Some classes in the Java SE class library implement Serializable, and others do not. Examples that don't include Thread, Process, I/O classes, and many Swing and AWT classes. If you are uncertain, check the javadocs for the respective classes.

The above restrictions are all implemented as runtime checks, though some IDEs and code checkers may give you build-time warnings.

For more details, refer to the Java Object Serialization specification and the javadocs for Serializable, Externalizable, ObjectInputStream and ObjectOutputStream.

There are some other obscure aspects that I haven't covered. There is also a requirement that compatible versions of all of the classes in a serialization are available to the JVM when you deserialize.


Note that it is NOT a requirement to provide a serialVersionUID field, though there are advantages in doing so.

Upvotes: 0

Rainbolt
Rainbolt

Reputation: 3660

The Javadoc will tell you if a class from Java standard library implements a particular interface. Look under All Implemented Interfaces. For example, the String class implements Serializable, CharSequence, and Comparable<String>.

If you create your own class, make it serializable like this:

public class MyClass implements Serializable {}

Your class can only be serializable if every object inside of it is also serializable. If your class contains a non-serializable object, most modern IDEs will alert you, or the compiler will simply throw an error when you try to build.

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

make your class implements Serializable interface

and don't forgot to add serialversionuid

Upvotes: 1

Smutje
Smutje

Reputation: 18123

If it implements Serializable.

Upvotes: 4

Related Questions