Java Reflection - Class.getFields() Examples

Code Snippets Java Reflection Java Java API 




public Field[] getFields()
                  throws SecurityException

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

the array of Field objects representing the public fields


The field anotherNumber defined in a super interface and in a super class with different type, but both are returned:

package com.logicbig.example.clazz;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;

public class GetFieldsExample {

public static void main(String... args) {
Class<MyClass> c = MyClass.class;
Field[] fields = c.getFields();

private static class MyClass extends MySuperClass implements MyInterface {}

private static class MySuperClass extends MySuperSuperClass {
public String aString;
private char cChar;
public BigInteger anInteger;
public BigInteger anotherNumber;

private static class MySuperSuperClass {
public double aDouble;
BigInteger anotherNumber;

private static interface MyInterface {
BigDecimal aNumber = BigDecimal.TEN;
BigInteger anInteger = BigInteger.ONE;
BigDecimal anotherNumber = BigDecimal.ZERO;



public static final java.math.BigDecimal com.logicbig.example.clazz.GetFieldsExample$MyInterface.aNumber
public static final java.math.BigInteger com.logicbig.example.clazz.GetFieldsExample$MyInterface.anInteger
public static final java.math.BigDecimal com.logicbig.example.clazz.GetFieldsExample$MyInterface.anotherNumber
public java.lang.String com.logicbig.example.clazz.GetFieldsExample$MySuperClass.aString
public java.math.BigInteger com.logicbig.example.clazz.GetFieldsExample$MySuperClass.anInteger
public java.math.BigInteger com.logicbig.example.clazz.GetFieldsExample$MySuperClass.anotherNumber
public double com.logicbig.example.clazz.GetFieldsExample$MySuperSuperClass.aDouble

See Also