Close

Java Reflection - Field.getType() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public Class<?> getType()

Returns a Class object identifying the declared type of this field.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetTypeExample {
private int anInt;

public static void main(String... args) throws NoSuchFieldException {
Field f = GetTypeExample.class.getDeclaredField("anInt");
Class<?> type = f.getType();
System.out.println(type);

}
}

Output

int




package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetTypeExample2 {
private int[] ints;

public static void main(String... args) throws NoSuchFieldException {
Field f = GetTypeExample2.class.getDeclaredField("ints");
Class<?> v = f.getType();
System.out.println(v);
String name = v.getTypeName();
System.out.println(name);
}
}

Output

class [I
int[]




See Also