7

Should I add the serialVersionUID field when creating an interface that extends Serializable?

My IDE (Netbeans 8.2) complains that the field is missing. However, to my understanding, serialVersionUID is only applicable to non-abstract classes (the specific classes that will be instantiated during deserialization).

If serialVersionUID is necessary, what should I do about interfaces that extend interfaces? Normally, this field is declared in every class down the class heirarchy. While this is also possible for interfaces, it leads to a different IDE warning that fields hide fields.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99

1 Answers1

6

No you should not. The serialVersionUID of an interface is nowhere taken into account during the serialization or deserialization process. It is pointless. serialVersionUID is for Serializable classes.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @Eugene No. Static variables are not serialized, and the `serialVersionUID` of a `Class` is given by `Class.serialVersionUID`. – user207421 Nov 25 '17 at 23:04
  • @Eugene The code in the comment is not legal Java, so it is pointless to consider it further; but `Class c` already has a `serialVersionUID` member, because it is an instance of `Class`. As I already stated. And this has exactly nothing to do with interfaces, which is what both the question and my answer are about. – user207421 Jun 04 '21 at 07:41
  • you're right. 4 years ago I did not see this, now I realize I was wrong. I can't upvote this twice unfortunately. – Eugene Jun 04 '21 at 19:26
  • With java 8, if your interface has any `default` methods, you will still need `@SuppressWarnings("serial")` due to https://bugs.openjdk.java.net/browse/JDK-8191637 – Archie Apr 08 '22 at 23:05