Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a transient field in a class be obtained using reflection

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

like image 797
java_geek Avatar asked Feb 13 '10 06:02

java_geek


3 Answers

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")?)

like image 197
Bozho Avatar answered Sep 18 '22 03:09

Bozho


transient indicates that the field will not be serialized. The field is still declared by the class, so it is fair game for reflection.

like image 32
akf Avatar answered Sep 20 '22 03:09

akf


Among all the objects that needs to be serialized there are those one that need not to be serialized. That's why this objects are marked with the keyword transient.

like image 38
G.Bonang Avatar answered Sep 17 '22 03:09

G.Bonang