Java: Should serializable inner & anonymous classes have SerialVersionUID?

后端 未结 1 1283
有刺的猬
有刺的猬 2021-01-06 04:18

Although I\'m not currently planning to serialize anything, I give all serializable outer classes, as well as static nested classes a SerialVersionUID, because

相关标签:
1条回答
  • 2021-01-06 05:03

    Give them a serialVersionUID, because:

    • It's good general practice and it certainly doesn't hurt to specify it.
    • Warnings should be addressed, not suppressed.
    • Sometimes inner classes are changed to be top-level classes when they get large enough.

    It's good (for all of the reasons stated in the documentation to which you've linked) that you won't be serializing instances of those inner classes. I suppose, if you were paranoid or worried other developers might not exercise the same good judgement, you could enforce that choice by having a writeObject method in each inner class that unconditionally throws an exception.

    0 讨论(0)
提交回复
热议问题