0

Short version:

What is the MOST appropriate access modifier for serialVersionUID? Eclipse defaults to private, but are there anything wrong with setting it to something more visible?

Background:

I'm writing a library that heavily extends ArrayList. I want Eclipse to throw a warning if I don't include serialVersionUID in a serialized class. I ALSO want the compiler to throw a warning if it encounters a field WITHOUT a javadoc comment. Finally, I want the editor to remove unused fields if they are not in use.

So to achieve this, I set my class body template to this

/**
 * Generated Serial Version UID
 */
public static final long serialVersionUID=7707109240879740918L;

Where the value of serialVersionUID is just an arbitrarily chosen random number that I've hardcoded into the template.

The field is public to get around the "unused variable issue".

The field has a hard-coded comment to get around the uncommented-field issue.

Yes, I know it's very picky, but I'm a lazy perfectionist who doesn't want to have to manually add that serialVersionUID each time and write the comment myself.

Sadly, I can't specify specific kinds of class templates that apply to only serialized classes, nor can I specify a template specific to the field serialVersionUID, hence the need to hard code it all and set to non-private to avoid the compiler complaining about the lack of comment AND the editor removing the unused value when I clean up my source code.

Whatever my reasons, have I introduced a potential bug or vulnerability in this class by setting serialVersionUID to anything other than private?

HesNotTheStig
  • 549
  • 1
  • 4
  • 9
  • 2
    what about keeping it private but adding `@SuprressWarnings("unused")`? Does that help? – yshavit Jul 10 '15 at 16:40
  • 1
    It should be private, because no other class will ever have a use for it. Whatever mechanism flags unused variables ought to know that serialVersionUID is special. I feel you should never write bad code just to make an IDE shut up about something it's too naïve to handle properly. – VGR Jul 10 '15 at 17:08
  • I know @SuppressWarnings("unused") exists, but I get this sick feeling in my stomach whenever I use something like that. The ideal solution would be for the Eclipse compiler to understand that field is not unused when private IF the class implemements Serializable. – HesNotTheStig Jul 17 '15 at 22:31

0 Answers0