Click to See Complete Forum and Search --> : Accessing Private Fields


Jutzi
May 3rd, 2000, 07:46 AM
I like to write a library for serializing an object into XML. For this I need to access (read & write)
private variables of a subclass (the objects has to be derived from my base class). Is there a possibility
to achieve this with reflection? Or is there an other solution for my problem?

Thanks a lot for answering my question!

kib63613
May 3rd, 2000, 07:57 PM
You may consider the method getDeclaredFields() in the class java.lang.Class. It return the fields
in the specified class. I adopt some words in the document :
Returns an array of Field objects reflecting all the fields declared by the class or interface
represented by this Class object. This includes public, protected, default (package) access,
and private fields, but excludes inherited fields.
So I guess that you might have to cast the base class to the subclass and get the field in the subclass.
I never try it. This is just the suggestion.
good luck,
Alfred Wu