views:

89

answers:

4

When i define a java object to implement serializable interface, do the members of the object, also become serializable? Or i have to go as far along the nesting depth, to redefine every object along the path as serializable?

+3  A: 

Well, implementing Serializable will give you serialization support only if all non-transient members (thanks, danben) are either primitives or serializable classes themselves.

So yes, if you have a lot of different things as members that are not serializable, then you have to make them serializable too.

Unless they are not important for representing your object's state. If you can re-create it without them, then you can always make the members transient to omit them from serialization.

Joey
All non-transient members, I believe.
danben
Ah, right. It's been a while.
Joey
The compiler doesn't check this unfortunately!
Neil Coffey
Hm, then it was a runtime exception or so.
Joey
unfortunatly NotSerializableException is thrown at runtime
stacker
Although the compiler doesn't check it, IDEs (IDEA for sure, probably Eclipse as well as others) can be configured to warn you.
Yishai
+4  A: 

Most classes that you use regularly in java are serializable (Collections, String, decedents of Number, etc.), however any classes that you reference either have to be serializable or declared transient. Of course, if they are transient, they won't be referenced when the class is deserialized.

Yishai
What about Lists ?
The Machine
Lists are collections.
Joey
It's essentially the same argument-- the standard JDK implementations like ArrayList are serializable per se, but if you try to serialize one that contains a non-serializable object then this will fail.
Neil Coffey
From the API: The List superinterface doesn't extend Serializable, but the commonly used ArrayList, Stack, LinkedList and Vector do all implement Serializable.
Lord Torgamus
Thanks a lot, couldn't find any documentation, or specification that states that.
The Machine
Lord Torgamus
A: 

The members are not automatically made Serializable.

If you have members that you have in your class and which you wrote yourself, you have to go to each and make them serializable (by implementing the interface).

Most of the types that come with JAVA libraries are already serializable, so that shouldn't bother you.

And ofcourse, this applies to all members recursively.

In other words - If there is a piece of data or value that needs to be transferred or saved, at any depth within an object, it has to be Serializable.

Ben
A: 

This component classes should also implement the Serializable interface, or an java.io.NotSerializableException will be thrown.

nont
Example: http://www.devx.com/tips/Tip/13020
Lord Torgamus