Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. String name;. Previous Lambda Expressions in Java 8.
Next Garbage Collection in Java. Recommended Articles. Article Contributed By :. Only objects that support the java. Serializable interface can be written to streams. In this example, we are going to serialize the object of Student class from above code. The writeObject method of ObjectOutputStream class provides the functionality to serialize the object.
We are saving the state of the object in the file named f. Deserialization is the process of reconstructing the object from the serialized state. It is the reverse operation of serialization.
Let's see an example where we are reading the data from a deserialized object. If a class implements Serializable interface then all its sub classes will also be serializable. Let's see the example given below:. Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be. If a class has a reference to another class, all the references must be Serializable otherwise serialization process will not be performed.
In such case, NotSerializableException is thrown at runtime. If there is any static data member in a class, it will not be serialized because static is the part of class not object. Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed. The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format.
It is not a marker interface. Now, id will not be serialized, so when you deserialize the object after serialization, you will not get the value of id. Learn more. When should we implement Serializable interface? Ask Question. Asked 10 years, 10 months ago. Active 2 years, 4 months ago. Viewed k times. Why do we do that? Does it give any advantages or security? Improve this question. OneCricketeer k 16 16 gold badges silver badges bronze badges.
FYI the accepted answer here is incomplete and misleading, because it doesn't address the security drawbacks.
See Effective Java , item Implement Serializable with great caution. Raedwald's answer here saying not to use serialization is the correct one. Add a comment. Active Oldest Votes.
Because you want to store or send an object. It makes storing and sending objects easy. It has nothing to do with security. Improve this answer. Kasun Siyambalapitiya 3, 6 6 gold badges 30 30 silver badges 53 53 bronze badges. These other object serialization mechanisms typically do not require your Java classes to implement Serializable , by the way. They are typically using Java Reflection to inspect your class, so implementing the Serializable interface would be superflous - it would not add any useful information.
Object serialization is kind of a topic in its own right. Therefore I will not get into deeper detail about object serialization at this point. In addition,a lot has already been written online about Java object serialization. Instead of repeating it, I'll give you a link to deeper explanations of the subject. Tutorials About RSS. Java IO.
0コメント