Alex Rivera | Logout

Can a transient field in a class be obtained using reflection

Asked 2010-02-13T06:41:57.923
12

Can a transient field in a class be obtained using reflection? (using getDeclaredField(..))

Edit
Report

1 Answer

28

Yes, It is a normal field. You can check whether it is transient by:

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.

So no logical reason for it not to be accessible by reflection. It's the value of the field that is ignored (sometimes), not the field itself.

(btw, what hindered you from just trying to call getDeclaredField("yourTransientField")?)

answered 2010-02-13T06:45:16.480

Your Answer