How to serialize static data members of a Java class?

后端 未结 9 1871
孤城傲影
孤城傲影 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:11

    Folks, static doesn't mean IMMUTABLE. For instance, I may want to serialize the whole state of the computation (yes, including static fields -- counters, etc) to resume later, after JVM and/or host computer restarted.

    The right answer to that, as already said, is to use Externalizable, not Serializable, interface. Then you have a complete control on what and how you externalize.

提交回复
热议问题