Java

Topic: Serialization

How to make a Java class Serializable?

Making a class Serializable in Java is very easy, Your Java class just needs to implements java.io.Serializable interface and JVM will take care of serializing object in default format. Decision to making a Class Serializable should be taken concisely because though near term cost of making a Class Serializable is low, long term cost is substantial and it can potentially limit your ability to further modify and change its implementation because like any public API, serialized form of an object becomes part of public API and when you change structure of your class by implementing addition interface, adding or removing any field can potentially break default serialization, this can be minimized by using a custom binary format but still requires lot of effort to ensure backward compatibility. One example of How Serialization can put constraints on your ability to change class is SerialVersionUID. If you don't explicitly declare SerialVersionUID then JVM generates its based upon structure of class which depends upon interfaces a class implements and several other factors which is subject to change. Suppose you implement another interface than JVM will generate a different SerialVersionUID for new version of class files and when you try to load old object object serialized by old version of your program you will get InvalidClassException.Read more: http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html#ixzz25PQe1Lgv

Browse random answers:

Define Serialization? What do you mean by Serialization in Java?
Why is Serialization required? What is the need to Serialize?
What is the Difference between Externalizable and Serializable Interfaces?
When will you use Serializable or Externalizable interface? and why?
What are the ways to speed up Object Serialization? How to improve Serialization performance?
What is a Serial Version UID (serialVersionUID) and why should I use it? How to generate one?
What would happen if the SerialVersionUID of an object is not defined?
Does setting the serialVersionUID class field improve Java serialization performance?
What are the alternatives to Serialization? If Serialization is not used, is it possible to persist or transfer an object using any other approach?
What are transient variables? What role do they play in Serialization process?
Why does serialization NOT save the value of static class attributes? Why static variables are not serialized?
How to Serialize a collection in java? How to serialize a ArrayList, Hashmap or Hashset object in Java?
Is it possible to customize the serialization process? How can we customize the Serialization process?
How can a sub-class of Serializable super class avoid serialization? If serializable interface is implemented by the super class of a class, how can the serialization of the class be avoided?
What changes are compatible and incompatible to the mechanism of java Serialization?
How to make a Java class Serializable?
How many methods Serializable has? If no method then what is the purpose of Serializable interface?
What is serialVersionUID? What would happen if you don't define this?
While serializing you want some of the members not to serialize? How do you achieve it?
 What will happen if one of the members in the class doesn't implement Serializable interface?
 If a class is Serializable but its super class in not, what will be the state of the instance variables inherited from super class after deserialization?
 Can you Customize Serialization process or can you override default Serialization process in Java?
 Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?
Which methods are used during Serialization and DeSerialization process in java?
Suppose you have a class which you serialized it and stored in persistence and later modified that class to add a new field. What will happen if you deserialize the object already serialized?
What are the compatible changes and incompatible changes in Java Serialization Mechanism?
Can we transfer a Serialized object vie network?
Which kind of variables is not serialized during Java Serialization?