Results 1 to 6 of 6
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default 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>

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    If you don't know what it is don't worry about it.
    My IP address is 127.0.0.1

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    static final long serialVersionUID = 0;
    add an 'L' after the 0 value...

    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

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    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

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    The serializable class SimpleFrame does not declare a static final serialVersionUID field of type long
    It means that the Class SimpleFrame have no serialVersionUID variable initialized.....

    You've initialized it at SimpleFrameTest.

    Try to cut it and paste in inside the SimpleFrame class.
    freedom exists in the world of ideas

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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:
    Java Code:
    private void readObject(ObjectInputStream in)
        throws ClassNotFoundException, IOException
    
    private void writeObject(ObjectOutputStream out) 
        throws IOException
    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.
    Last edited by danielstoner; 05-04-2008 at 08:18 PM.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •