What is Serializable in Java? [duplicate]
Possible Duplicat开发者_运维技巧e:
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 {
}
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.
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
.
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.
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.
Basically it's a marker interface that says your class can be serialized. See here for more info.
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?
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.
精彩评论