Question

This has become a pet peeve of mine. I write a class, and implement Serializible. Then eclipse warns me that I don't have a serialVersionUID, so I select "Add generated serialVersionUID" or "Add default serialVersionUID" and I end up with something like this:

  /**
   * 
   */
  private static final long serialVersionUID = 4049849541314027178L;

Most of the time I don't want to add a comment, so I have to go and delete the comment. I would rather the default be no comment, but I've looked through the code templates in preferences and haven't figured out how to change this. I simply want it to look like this:

  private static final long serialVersionUID = 4049849541314027178L;
Was it helpful?

Solution

It's using the template for any eclipse-generated field.

You can change it in

 Preferences -> 
     Java -> 
        Code Style -> 
           Code Templates -> 
              Comments -> 
                 Fields

... either globally or per-project.

OTHER TIPS

To explain it further from the first answer, the auto-generated comments for the serialVersionUID is simply a comment template generation that applies to all fields, including this one. It runs when you generate element comment (Alt+Shift+J) for that field from the Source section of the Toolbar.

The important fact is that the generation of field comments also runs automatically when you quick-fix

The serializable class [ClassName] does not declare a static final serialVersionUID field of type long"

Disabling/changing generated comments for all fields

You can prevent the generation of comments for all fields by clicking edit and blanking the comment after performing what the original answerer @slim answered, as in

Toolbar -> Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Fields

After quick-fixing the serializable warning, note that the comment is no longer auto-generated.

Disabling/changing the automatic generation of the comment just for SerialVersionUID

Unfortunately, there is no option to prevent the auto-generation of comments for the SerialVersionUID through quick-fixing without blanking the comment generation template for all fields. There is no such option when searching for "Comment" or "Serial" in Preferences.

Fortunately, it doesn't matter much since for other fields, it generates only when you generate element comment for that field. And you can also prefer to comment or Javadoc fields manually.

Example

This code excerpt (only fields shown) shows that warning about serialization.

public class SomePanel extends JPanel {
    private String name;
    /* ... */
}

You quick-fix this warning, and you generate element comment the name field. Without blanking the template, you get this, which you do not want for serialVersionUID:

public class SomePanel extends JPanel {
    /**
     *
     */
    private static final long serialVersionUID = -5173652620102412871L;
    /**
     * 
     */
    private String name;
    /* ... */
}

After blanking the template for comment generation of fields, you get, as expected:

public class SomePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private String name;
    /* ... */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top