Set null to a field value
import java.lang.reflect.Field;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
Bean demo = new Bean();
Class clazz = demo.getClass();
Field field = clazz.getField("id");
field.set(demo, new Long(10));
Object value = field.get(demo);
System.out.println("Value = " + value);
field = clazz.getField("now");
field.set(null, new Date());
value = field.get(null);
System.out.println("Value = " + value);
}
}
class Bean {
public static Date now;
public Long id;
public String name;
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Reflection Field:
- Get all fields
- Get all Declared Fields
- Get annotations for a Field
- Get "public static final" field
- Get specific fields
- Get Field value by field name
- Get fields for super class
- Get Inherited Methods and fields
- Get Type of the field or return type of a method.
- Field modifiers: isSynthetic, isEnumConstant
- Set field value
- Set null to a field value
- Set private field value