21

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?

Community
  • 1
  • 1
danpker
  • 593
  • 1
  • 5
  • 15

7 Answers7

41

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).

Faahmed
  • 375
  • 4
  • 21
Michael Neale
  • 19,248
  • 19
  • 77
  • 109
28

Java Serialization-----Have you ever seen what is inside a serialized object? I will explain you what is java serialization, then provide you with a sample for serialization. Finally most importantly, lets explore what is inside a serialized object and what it means. That is internals of java serialization and how does it works. If you want to have your own implementation of java serialization, this article will provide you with a good platform to launch.

What is Java Serialization? Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.

Why I used ‘primary purpose’ in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.

How do you serialize? When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.

Here protocol means, understanding between serializing person and de-serializing person. What will be the contents of file containing the serialized object?

renu
  • 289
  • 3
  • 2
  • 26
    Is it copied from this link??? At least try changing the words. http://javapapers.com/core-java/java-serialization/ – Konza May 07 '13 at 04:23
7

Serialization is writting the object into a form which is readable and allows the object to be re-created at a different time. So if I created a widget on computer A under one JVM, serialized and saved it and sent it to computer B running a different, the other JVm would be capable of de-serializing it and re-creating it with the same values and structure

bogertron
  • 2,255
  • 1
  • 24
  • 36
6

Java Serialisation is a way to persist object structures.

It is best practice for serialisable class to declare serialVersionUID as a private static final long compile-time constant. This is used to check that object data and class code are claimed to be compatible.

So why is Eclipse telling you about this? Probably, the class that you are extending (or potentially interface you are implementing) implements java.io.Serializable. That means that all subtypes, including yours are serializable. Almost certainly you don't care. You should be able to clear the warnings by applying @SuppressWarnings("serial") on the class or package (in package-info.java). If you want to forcibly prevent instances of your class being serialised, then add (from memory):

private static final java.io.ObjectStreamField[] serialPersistentFields = {
    null
};
private void writeObject(
    java.io.ObjectOutputStream ou
) throws java.io.IOException {
    throw new java.io.NotSerializableException();
}
private void readObject(
    java.io.ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
    throw new java.io.NotSerializableException();
}
private void readObjectNoData(
) throws java.io.ObjectStreamException {
    throw new java.io.NotSerializableException();
}

It's probably not the best thought out system in the world (although it is much better than many people give it credit for).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
5

Serialization is the process of converting an object to a disk-storable format, for re-loading it at a later time. Check the link for more information!

Stephen
  • 18,827
  • 9
  • 60
  • 98
James B
  • 8,183
  • 4
  • 33
  • 40
3

Serialization is a way to take an object (an instance of your class) and set it up for transport--across a network, to disk, etc.

Alan
  • 45,915
  • 17
  • 113
  • 134
2

Serialization is process of writing an representation of object's instance to a stream (or, to a sequence of bytes). See what Sun says about it: http://java.sun.com/developer/technicalArticles/Programming/serialization/

david a.
  • 5,283
  • 22
  • 24