Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization in java

When i define a java object to implement serializable interface, do the members of the object, also become serializable? Or i have to go as far along the nesting depth, to redefine every object along the path as serializable?

like image 851
The Machine Avatar asked Jan 19 '10 15:01

The Machine


People also ask

What is serialization with example in Java?

Serialization in Java is the process of converting the Java code Object into a Byte Stream, to transfer the Object Code from one Java Virtual machine to another and recreate it using the process of Deserialization.

What is serialization and why it is used?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is the use of serializable interface in Java?

Serializable interface. Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.

Which keyword is used serialization in Java?

Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies. The reverse operation of serialization is called deserialization where byte-stream is converted into an object.


2 Answers

Most classes that you use regularly in java are serializable (Collections, String, decedents of Number, etc.), however any classes that you reference either have to be serializable or declared transient. Of course, if they are transient, they won't be referenced when the class is deserialized.

like image 81
Yishai Avatar answered Oct 04 '22 00:10

Yishai


Well, implementing Serializable will give you serialization support only if all non-transient members (thanks, danben) are either primitives or serializable classes themselves.

So yes, if you have a lot of different things as members that are not serializable, then you have to make them serializable too.

Unless they are not important for representing your object's state. If you can re-create it without them, then you can always make the members transient to omit them from serialization.

like image 32
Joey Avatar answered Oct 04 '22 01:10

Joey