How do I list the declared fields of a class?
Author: Deron Eriksson
Description: This Java tutorial describes how to use the Class object's getDeclaredFields() method.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4

In JavaSW, it's easy to list the declared fields of a class. If you have an object, you can obtain its Class object by calling getClass() on the object. You can then call getDeclaredFields() on the Class object, which will return an array of Field objects. This list can include public, protected, and private fields.

This is illustrated in the ClassFieldTest class.

package test;

import java.lang.reflect.Field;

public class ClassFieldTest {

	public static void main(String args[]) {
		FieldTest ft = new FieldTest();
		Class ftClass = ft.getClass();

		Field[] fields = ftClass.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			System.out.println("declared field: " + fields[i]);

The FieldTest class is shown below.

package test;

public class FieldTest {

	private String pri;
	protected String pro;
	public String pub;

	public FieldTest() {

	public FieldTest(String pri, String pro, String pub) {
		this.pri = pri; = pro; = pub;


If we execute the ClassFieldTest class, we see that the declared fields of the FieldTest class are displayed. The listed fields can be public, protected, and private.

declared field: private java.lang.String test.FieldTest.pri
declared field: protected java.lang.String
declared field: public java.lang.String