How to get protected fields

Hello,

I don't know how to get protected fields that a class inherits from its superclass through reflexion ... the getDeclaredFields method seems not working ...

here is my test program:

import java.io.File;

import java.lang.reflect.Field;

public class Test {

public static void main(String[] args) {

Field [] fieldList = SubClass.class.getDeclaredFields();

for (Field field: fieldList) {

field.setAccessible(true);

System.out.println (field.getName());

}

}

static class SuperClass {

protected File file;

public SuperClass () {

file = new File ("a.txt");

}

}

static class SubClass {

private File myfile;

public SubClass () {

myfile = new File ("b.txt");

}

}

}

The output is

myfile

This means, the getDeclaredField method of the SubClass returns only fields declared by this class, not protected fields declared by its superclass.

Anyone has an idea to fix the problem, please help me. Thank you a lot

}

[1092 byte] By [mayxanha] at [2007-11-27 3:08:30]
# 1
That's not a problem, the documentation specifically says that getDeclaredFields() only returns fields from this class. If you want fields from the superclass, why not just getSuperclass().getDeclaredFields()?
DrClapa at 2007-7-12 3:56:38 > top of Java-index,Core,Core APIs...
# 2
thank you very much ..
mayxanha at 2007-7-12 3:56:38 > top of Java-index,Core,Core APIs...