Eclipse: Ditch the 1L in your serialVersionUID

For those on the old school eclipse versions and not the privileged on new versions of eclipse…there isn’t the fast ctrl-1 to get rid of the unimplemented serialVersionUID error.

So you have a class like

package com.myClasses;
public class FooXxx implements Serializable {
private static final long serialVersionUID = 1L;
public FooXxx() {}
}

which got rid of the error with, “serialVersionUID error,” but not all your classes should be using “1L” as their key, then:

  1. remove the [private static final long serialVersionUID = 1L;] line
  2. save the file
  3. compile the file
  4. on the command line where JAVA_HOMEbin is in your path>
    1. navigate the file system to the root target directory of your class files
    2. serialver -classpath . com.myClasses.FooXxx
    3. it echos back [private static final long serialVersionUID = -5947083671915636940L;]
  5. copy the key and output line back into your class file and save.

Good guidelines on when to generate this:

  • This site on serialVersionUID generation guidelineshas some good recommendations:
    • part of which is
      Guidelines for serialVersionUID :
      
          * always include it as a field, for example: "private static final long serialVersionUID = 7526472295622776147L; " include this field even in the first version of the class, as a reminder of its importance
          * do not change the value of this field in future versions, unless you are knowingly making changes to the class which will render it incompatible with old serialized objects
          * new versions of Serializable classes may or may not be able to read old serialized objects;  it depends upon the nature of the change; provide a pointer to Sun's guidelines for what constitutes a compatible change, as a convenience to future maintainers
    • Hers’s the link for Oracle site on Serializable compatibility
It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply