Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is serialization in Java? [duplicate]

Tags:

Possible Duplicate:
What is object serialization?

I've made a small RSS Reader app using Swing and Eclipse keeps telling me "The serializable class MochaRSSView does not declare a static final serialVersionUID field of type long"

What is serialization and what benefits would it have?

like image 662
danpker Avatar asked Mar 03 '09 23:03

danpker


People also ask

What is serialization and cloning in Java?

So in a nutshell the clone() method serializes the object into byte array by calling serialize(object) and then converts that byte array into a object by calling deserialize() with it. It results in a new Object with the same properties as the object .

What is serialization with example in Java?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. The byte stream created is platform independent.

What is the main purpose of serialization?

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.


1 Answers

Serializable is a marker interfaces that tells the JVM it can write out the state of the object to some stream (basically read all the members, and write out their state to a stream, or to disk or something). The default mechanism is a binary format. You can also use it to clone things, or keep state between invocations, send objects across the network etc.

You can let eclipse generate one for you (basically just a long random but unique ID). That means you can control when you think a class would be compatible with a serialized version, or not.

(Note: that all the non transient member variables must be of a serializable class, or you will get an error - as the JVM will recurse through the structure writing out the state of each object down to the level of writing primitives to the ObjectOutputStream).

like image 131
Michael Neale Avatar answered Oct 02 '22 14:10

Michael Neale