How to serialize static data members of a Java class?

后端 未结 9 1864
孤城傲影
孤城傲影 2020-11-27 17:30

When we serialize objects, static members are not serialized, but if we need to do so, is there any way out?

9条回答
  •  [愿得一人]
    2020-11-27 18:15

    The first question is why you need to serialize the static members?

    Static members are associated with the class, not the instances, so it does not make sense to include them when serializing an instance.

    The first solution is to make those members not static. Or, if those members are the same in the original class and the target class (same class, but possibly different runtime environments), don't serialize them at all.

    I have a few thoughts on how one could send across static members, but I first need to see the use case, as in all cases that means updating the target class, and I haven't found a good reason to do so.

提交回复
热议问题