views:

1230

answers:

6

I've been learning how to use Serializable.

I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable.

But, who is actually implementing those two methods to serialize? Does Object take care of everything or different classes overloads them when necessary?

A: 

If you implement a class which has to be serializable, you also have to provide a method which does the serialization in the same class.

You cannot rely on Object to be able to guess what your class needs to be successfully serialized and deserialized. Think of working variables of your class for example which do not need to be serialized, Object would not be able to differentiate those from the important fields.

Kosi2801
That's what the transient keyword is for: to skip those fields when serializing an object.
Chochos
The default serialization is usually correct about what to do. If there are 'unimportant' fields, why even have them in your object at all?
Jorn
What do you mean by "have to"?
Michael Myers
Um ... no. Implement Serializable and if all your fields are serializable then so is your class. Talking about Java, right?
Robert Munteanu
Completely untrue; you can serialize just fine without implementing anything yourself.
Michael Borgwardt
+5  A: 

I suppose the methods you are talking about are readObject() and writeObject(). You only need to implement those if you need to do custom serialization, for example when you have fields in your object that aren't serializable. If you only have serializable fields and primitives, you don't have to implement custom serialization methods. Also, you can skip some fields on serialization by adding the transient keyword to them.

Jorn
+8  A: 

The serialization is actually implemented in java.io.ObjectOutputStream (and java.io.ObjectInputStream) and some of its helper classes. In many cases, this built-in support is sufficient, and the developer simply needs to implement the marker interface Serializable. This interface is called a "marker" because it doesn't declare any methods, and thus doesn't require any special API on implementation classes.

A programmer can add or replace default serialization mechanism with their own methods if needed. For example, if some additional initialization is required after deserializing an object, a method can be added with the following signature:

private void readObject(java.io.ObjectInputStream s)
               throws java.io.IOException, java.lang.ClassNotFoundException

For total control over serialization and deserialization, implement java.io.Externalizable instead of Serializable.

There are many other extension points in Java serialization, if needed. The serialization specification is an authoritative and complete source for learning about all of them.

erickson
Thanks. I think I was misunderstood. What I was really asking is: if my class implements serializable, who takes care of implementing the serialization methods."... The serialization is actually implemented in java.io.ObjectOutputStream (and java.io.ObjectInputStream) and some of its helper classes..."looks like the correct answer.Thanks erickson!
Macarse
+1  A: 

I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable.

Yes, this moment your class is Serializable.

Robert Munteanu
The quote includes 'different variables who implement Serializable'
Robert Munteanu
+2  A: 

Look at the API doc of Serializable, it explains the mechanism in detail.

Basically, you don't have to do anything unless you want more control over how your objects are serialized, in which case there are some "magic" methods you can implement and which will be called by the serialization mechanism.

If you want full control, you can use Externalizable instead.

Michael Borgwardt
+1  A: 

For a class to be serializable, each object contained as a member of that class must also be serializable. Java will run down the tree of all objects referenced by yours and serialize them each in turn.

If you want to have greater control over how objects are serialized, you can implement the Externalizable interface:

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.

Cuga