Java Reflection - Java Field.getAnnotation(Class <T> annotationClass)


Field.getAnnotation(Class <T> annotationClass) has the following syntax.

public <T extends Annotation> T getAnnotation(Class <T> annotationClass)


In the following code shows how to use Field.getAnnotation(Class <T> annotationClass) method.

//from w  w w. jav a2s  .  co  m
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

public class Main {

  public static void main(String[] args) {
    Class d = DataBean.class;

    Field fs[] = d.getFields();
    for (Field f : fs) {

      Annotation a = f.getAnnotation(DataField.class);

      if (a != null) {

class DataBean {
  public String name;

  public String data;

  public String description;

@interface DataField {

The code above generates the following result.