Re: jdk 1.5 question
gg wrote:
Aha, I just had to declare something in the class like
static final long serialVersionUID = 456156789;
I started using the hashmap. the only problem is that I got unsafe
operation because I have not understand how to create a hashmap with
something called sterilization ID. Have to look that up
Please do not top-post, that is, do not place answers above the quoted
material. Place your replies inline; it makes the post easier to read.
3
The serialVersionUID can be any long and it tells the serialization mechanism
which version of the class's serialization behavior to expect. That way if
you change your serialization (e.g., mark some fields transient), you change
serialVersionUID to a different value so programs won't try to deserialize an
incompatible version of the class.
You don't actually need to provide a serialVersionUID, although it is good
practice to do so. The system will calculate a default value for you when you
compile the class.
BTW, place the "L" suffix after the numeric literal to make it a long. It
saves a conversion step, but more importantly it documents your intent that
the literal is a long.
You should read
<http://java.sun.com/javase/6/docs/api/java/io/Serializable.html>
and Joshua Bloch's book /Effective Java/, with its excellent chapters on
serialization, before attempting to use the feature.
Study first, act second.
Next problem - why are you being told to provide a serialization ID? Instead
of using your "hashmap" class, why not use java.util.HashMap,
<http://java.sun.com/javase/6/docs/api/java/util/HashMap.html>,
which already implements Serializable?
That way you will not need to create a serialVersionUID, implement
private void writeObject(java.io.ObjectOutputStream out)
throws IOException;
or
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
or do any of that messy stuff.
--
Lew