The following methods are available to retrieve fields of a class:
public Field getDeclaredField(String name) public Field[] getDeclaredFields() public Field getField(String name) public Field[] getFields()
To get fields including inherited ones, use getFields()
method instead.
Note that getFields()
methods only return public inherited methods.
import java.lang.reflect.Field; public class Main { public static void main(String args[]) throws Exception { Class c = Class.forName("java.lang.String"); Field[] fields = c.getFields(); System.out.println("No of fields : " + fields.length); for (Field f : fields) System.out.println(f);/*from www .j a v a 2s. c o m*/ } }