Java: Should serializable inner & anonymous classes have SerialVersionUID?

末鹿安然 提交于 2019-11-30 22:25:01

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!