Results 1 to 6 of 6
Thread: serialVersionUID
- 05-04-2008, 03:43 PM #1
serialVersionUID
When compiling the code below the following warning is displayed:
The serializable class SimpleFrame does not declare a static final serialVersionUID field of type long
Having declared it the warning re-appears. Any idea what Eclipse 3.1.1 is expecting?
<CODE>
import javax.swing.*;
/**
* @version 1.32 2007-06-12
* @author Cay Horstmann
*/
public class SimpleFrameTest
{
static final long serialVersionUID = 0; //what format is commonly used here?
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
</CODE>
- 05-04-2008, 04:10 PM #2
If you don't know what it is don't worry about it.
My IP address is 127.0.0.1
- 05-04-2008, 04:49 PM #3
add an 'L' after the 0 value...static final long serialVersionUID = 0;
eg. static final long serialVersionUID = 0L;
The compiler asks about the constant.
Is it an int, float, byte, short or long datatype?freedom exists in the world of ideas
- 05-04-2008, 05:39 PM #4
It's a long value. Still the warning will remain a nuisance :-)
My understanding is that this is a version control mechanism used in serialization/deserialization of objects to/from external sources (e.g. xml files, databases) to classes. Presumably this concerns mostly J2EE (server) programming.
"In anticipating the need to evolve a serializable class, Java serialization provides a serialVersionUID, also called suid,
in the ObjectStreamClass for version control. suid is used to inform the Java serialization mechanism which version of
the class is compatible with this serialized object. However, the importance of this field is often overlooked,
resulting in release incompatibility."
Ensure proper version control for serialized objects - Java World
- 05-04-2008, 05:59 PM #5
It means that the Class SimpleFrame have no serialVersionUID variable initialized.....The serializable class SimpleFrame does not declare a static final serialVersionUID field of type long
You've initialized it at SimpleFrameTest.
Try to cut it and paste in inside the SimpleFrame class.freedom exists in the world of ideas
- 05-04-2008, 08:16 PM #6
Every class implementing Serializable or extending a class that implements Serializable has a class version number generated by the compiler unless you specify the version number using serialVersionUID. This is useful when you serialize objects on the disk or across the network. In the first scenario you can imagine a "disconnect" between the serialized object on the disk and the class who tries to reload it. The instance can be written on the disk by one version of the class and loaded by a new version of the class let say after an upgrade. The same is true for the network scenario, the class sending can be at a different version than the class receiving.
If your class is not involved in one of these two scenarios, then just declaring the serialVersionUID = 1L is enough. If you actually use the serialization mechanism then you have to do more work in order to ensure your code doesn't break when you change the Serializable classes. This will involve implementing your own version of readObject and writeObject methods:
By the way, the format of the serialVersionUID value is up to you. Eclipse can generate a random number for you but it is better to use something simple like 1L, 2L etc or something like 20080504L. It is always better to have a controlled format.Java Code:private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException private void writeObject(ObjectOutputStream out) throws IOExceptionLast edited by danielstoner; 05-04-2008 at 08:18 PM.
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks