An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface — it provides some functionality serialization mechanism to verify that the class can be persisted, typically to a file.
Transient variables cannot be serialized. The fields marked transient in a serializable object will not be transmitted in the byte stream.
An example would be a file handle, a database connection, a system thread etc. Such objects are only meaningful locally. So they should be marked as transient in a serializable class.
1. Depends on reflection
2. Has an incredibly verbose format.
3. Is very easy to send surplus data.
A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database. Deep cloning or the copy can be achieved through the serialization. This may be fast to code but will have performance implications.
The objects stored in an HTTP session should be serializable to support in-memory replication of sessions to achieve scalability.
Objects are passed in RMI(Remote Method Invocation) across the network using serialization.
This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of the addition of the new field. You can avoid this exception being thrown by controlling the versioning yourself by declaring an explicit serialVersionUID.
There is a small performance benefit in explicitly declaring your serialVersionUID(because does not have to be calculated).
So it is best practice to add your own serialVersionUID to your Serializable classes as soon as you create them as shown below:
public Class Car{ static final long serialVersionUID =1L; //assign a long value }
Amit is a software developer by profession. He is passionate about all the technologies which can be used to build an application like java related technologies and many more and loves to implement it his career or for knowledge purpose and share his experience with you all. To know more see the About me profile.