Field: Class getType() : Field « java.lang.reflect « Java by API






Field: Class getType()

  

import java.lang.reflect.Field;

public class Main {

  public static void initIntFields(final Object obj) {
    try {
      Field[] fields = obj.getClass().getDeclaredFields();
      for (int idx = 0; idx < fields.length; idx++) {
        if (fields[idx].getType() == int.class) {
          fields[idx].setAccessible(true);
          fields[idx].setInt(obj, 0);
        }
      }
    } catch (final IllegalAccessException ex) {
      throw new RuntimeException(ex);
    }
  }

  public static final void main(final String[] args) {
    Integer value = new Integer("123");
    System.out.println("Before: " + value);
    initIntFields(value);
    System.out.println("After: " + value);
  }
}

   
    
  








Related examples in the same category

1.Field: get(Object obj)
2.Field: getAnnotation(Class annotationClass)
3.Field: getDouble(Object obj)
4.Field: getGenericType()
5.Field: getInt(Object obj)
6.Field: getModifiers()
7.Field: isEnumConstant()
8.Field: isSynthetic()
9.Field: setAccessible(boolean flag)
10.Field: setBoolean(Object obj, boolean z)
11.Field: setInt(Object obj, int i)