Java Reflection - Java Field.setLong(Object obj, long l)


Field.setLong(Object obj, long l) has the following syntax.

public void setLong(Object obj,  long l)  throws IllegalArgumentException ,   IllegalAccessException


In the following code shows how to use Field.setLong(Object obj, long l) method.

import java.lang.reflect.Field;
//from w  w  w . ja v a  2 s.  co  m
class MyClass {
  public long i = 99999999L;

public class Main {
  public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    f.setLong(x, 9L);


The code above generates the following result.