如何序列化 Java 类的静态数据成员? [英] How to serialize static data members of a Java class?

查看:31
本文介绍了如何序列化 Java 类的静态数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们序列化对象时,静态成员并没有被序列化,但是如果我们需要这样做,有什么办法吗?

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

推荐答案

第一个问题是为什么需要序列化静态成员?

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.

这篇关于如何序列化 Java 类的静态数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆