类中的transient
字段能否通过反射获取? (使用 getDeclaredField(..)
)
最佳答案
是,这是一个普通字段。您可以通过以下方式检查它是否是 transient 的:
Modifier.isTransient(field.getModifiers());
transient: A keyword in the Java programming language that indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are included.
因此没有合乎逻辑的理由不能通过反射访问它。被忽略的是字段的值(有时),而不是字段本身。
(顺便说一句,是什么阻碍了您尝试调用 getDeclaredField("yourTransientField")
?)
关于java - 是否可以使用反射获取类中的 transient 场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2256734/