package com.logicbig.example.clazz;
import java.lang.reflect.Field;
public class GetFieldExample {
public int aNumber;
public static void main(String... args) throws NoSuchFieldException {
Class<GetFieldExample> c = GetFieldExample.class;
Field field = c.getField("aNumber");
System.out.println(field);
Class<?> type = field.getType();
System.out.println(type);
}
}
Output
public int com.logicbig.example.clazz.GetFieldExample.aNumber
int
getField() method only works for public fields.
package com.logicbig.example.clazz;
import java.lang.reflect.Field;
public class GetFieldExample2 {
private int aNumber;
public static void main(String... args) throws NoSuchFieldException {
Class<GetFieldExample2> c = GetFieldExample2.class;
Field field = c.getField("aNumber");
System.out.println(field);
Class<?> type = field.getType();
System.out.println(type);
}
}
Output
Caused by: java.lang.NoSuchFieldException: aNumber
at java.base/java.lang.Class.getField(Class.java:1956)
at com.logicbig.example.clazz.GetFieldExample2.main(GetFieldExample2.java:16)
... 6 more
Field is searched in super classes/interfaces. Field in an interface will win if it is also in a super classes (with same name, regardless of type):
package com.logicbig.example.clazz;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
public class GetFieldExample3 {
public static void main(String... args) throws NoSuchFieldException {
Class<MyClass> c = MyClass.class;
Field field = c.getField("aString");
System.out.println("field for aString:\n " + field);
System.out.println("aString is declared in:\n " + field.getDeclaringClass());
field = c.getField("aNumber");
System.out.println("field for aNumber:\n " + field);
System.out.println("aNumber is declared in:\n " + field.getDeclaringClass());
field = c.getField("anInteger");
System.out.println("field for anInteger:\n " + field);
System.out.println("anInteger is declared in:\n " + field.getDeclaringClass());
field = c.getField("anotherNumber");
System.out.println("field for anotherNumber:\n " + field);
System.out.println("anotherNumber is declared in:\n " + field.getDeclaringClass());
}
private static class MyClass extends MySuperClass implements MyInterface {}
private static class MySuperClass {
public String aString;
public BigInteger anInteger;
public BigInteger anotherNumber;
}
private static interface MyInterface {
BigDecimal aNumber = BigDecimal.TEN;
BigInteger anInteger = BigInteger.ONE;
BigDecimal anotherNumber = BigDecimal.ZERO;
}
}
Output
field for aString:
public java.lang.String com.logicbig.example.clazz.GetFieldExample3$MySuperClass.aString
aString is declared in:
class com.logicbig.example.clazz.GetFieldExample3$MySuperClass
field for aNumber:
public static final java.math.BigDecimal com.logicbig.example.clazz.GetFieldExample3$MyInterface.aNumber
aNumber is declared in:
interface com.logicbig.example.clazz.GetFieldExample3$MyInterface
field for anInteger:
public static final java.math.BigInteger com.logicbig.example.clazz.GetFieldExample3$MyInterface.anInteger
anInteger is declared in:
interface com.logicbig.example.clazz.GetFieldExample3$MyInterface
field for anotherNumber:
public static final java.math.BigDecimal com.logicbig.example.clazz.GetFieldExample3$MyInterface.anotherNumber
anotherNumber is declared in:
interface com.logicbig.example.clazz.GetFieldExample3$MyInterface