Who actually implements serializable methods?
Asked Answered
B

6

16

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?

Baggs answered 9/6, 2009 at 15:45 Comment(0)
M
15

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.

Mcnair answered 9/6, 2009 at 15:57 Comment(1)
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!Baggs
M
7

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.

Morbidezza answered 9/6, 2009 at 15:54 Comment(0)
O
3

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.

Octodecimo answered 9/6, 2009 at 16:6 Comment(0)
K
1

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.

Karwan answered 9/6, 2009 at 15:58 Comment(1)
The quote includes 'different variables who implement Serializable'Karwan
S
1

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.

Sharynshashlik answered 9/6, 2009 at 16:7 Comment(0)
R
-1

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.

Rundell answered 9/6, 2009 at 15:52 Comment(5)
That's what the transient keyword is for: to skip those fields when serializing an object.Pylorectomy
The default serialization is usually correct about what to do. If there are 'unimportant' fields, why even have them in your object at all?Morbidezza
What do you mean by "have to"?Carolynncarolynne
Um ... no. Implement Serializable and if all your fields are serializable then so is your class. Talking about Java, right?Karwan
Completely untrue; you can serialize just fine without implementing anything yourself.Octodecimo

© 2022 - 2024 — McMap. All rights reserved.