11

Possible Duplicate:
What does Serializable mean?

I have

class Person implements Serializable {
}

what is the use of that and what will happen if I simply use

class Person {
}
Community
  • 1
  • 1

7 Answers7

16

serializable is a special interface that specifies that class is serialiazable. It's special in that unlike a normal interface it does not define any methods that must be implemented: it is simply marking the class as serializable. For more info see the Java docs.

As to what "serializable" means it simply means converting an instance of a class (an object) into a format where it can be written to disk, or possibly transmitted over a network. You could for example save your object to disk and reload it later, with all the field values and internal state saved. See the wikipedia page for more info.

Richard H
  • 38,037
  • 37
  • 111
  • 138
  • 2
    but why there is separate interface is made. I mean what was the harm in making evrything serializable. i mean from definition it does not look bad to have it at all the time . i mean is there any case where if i implement it and it will do some damagae and thats why java people made it optional. Eventually evrything is going to be on disk –  Mar 10 '11 at 14:45
  • 4
    If you're new to java. This is a practical application of polymorphism. If a class implements an interface then that class can be passed on to anywhere its parent can be accepted as arguments. In this case the child class could be passed on to functions like Results.writeOutput(Serializable s) – nanospeck Jun 02 '14 at 09:51
  • @nanospeck tl;dr : it helps us be lazy. I had to read this comment twice, but then it sunk in. Okay, that's pretty cool. So the point is like usual that we're getting code reuse for free, and almost everything you've ever written in code is similar enough to something someone else wrote so you can pass your object to someone else's code and leverage their code. Guessing this is especially useful if you're actually the person contributing the Java source code itself, reading through abstract stuff like CodecFactory(s) or something – Nathan majicvr.com Jan 10 '23 at 00:29
  • For other readers, @nanospeck 's comment was MUCH more helpful than either of the previous ones. I didn't end up reading Richard H's link thoroughly, but the simple example really helped clarify it for me – Nathan majicvr.com Jan 10 '23 at 00:34
  • Ack! @RichardH 's comment's link is now broken. That article is now preserved at http://web.archive.org/web/20110525003427/http://java.sun.com/developer/technicalArticles/Programming/serialization/ His original text: "because for certain objects it does not make sense for them to be serializable. There are also performance considerations. Read this for for info: [Richard's original link, which is now broken]" – Nathan majicvr.com Jan 10 '23 at 00:42
8

If you never serialize an instance of Person, there is no point in declaring implements Serializable. But if you don't and try to serialize an instance, you'll get a NotSerializableException.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

Serilaization ensures data can sent across the network and can be persisted and restored back to its original state using the serialization/de-serialization mechanism.

Deepak
  • 2,094
  • 8
  • 35
  • 48
2

This is a marker interface to declare this class as serializable. You should google for "java serialization" as this topic is sufficiently covered by hundreds of tutorials and articles. You could even start right at Wikipedia. In a nutshell, serialization is about reading and writing whole object graphs from/to streams like files or network sockets.

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
1

Basically it's a marker interface that says your class can be serialized. See here for more info.

lindsten
  • 88
  • 1
  • 6
1

Serializable is only a marker interface. It is completely empty.It simply allows the serialization mechanism to verify that the class is able to be persisted.

Also see following Why Java needs Serializable interface?

Community
  • 1
  • 1
ajm
  • 12,863
  • 58
  • 163
  • 234
  • everyone mentions serialize but what does that word actually means?? –  Mar 10 '11 at 14:30
  • @Pasha serialization is the process of converting a data structure or object into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. – ajm Mar 10 '11 at 14:36
0

The J2SE doc says:

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Basically, it's the interface that you have to implement for serialize classes in java.

Agusti-N
  • 3,956
  • 10
  • 40
  • 47