tags:

views:

1911

answers:

7

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?

+1  A: 

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
+4  A: 

Serializable is a marker interfaces that tells the JVM is 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).

Michael Neale
A: 

Serialisation is the process of converting an object to a disk-storable format, for re-load at a later time. Check the link for more info!

James Burgess
A: 

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
A: 

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.
+1  A: 

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
A: 

For more information http://www.google.co.uk/search?q=what+is+java+serialization 275,000 hits.

Peter Lawrey