Java Reflection - Java Field.setChar(Object obj, char c)








Syntax

Field.setChar(Object obj, char c) has the following syntax.

public void setChar(Object obj,  char c)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.setChar(Object obj, char c) method.

//from  w  ww  . j a  v a 2 s . c om
import java.lang.reflect.Field;

class MyClass {
  public char i = 'c';
}

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");
    System.out.println(f.getChar(x));
    
    f.setChar(x, 'a');
    System.out.println(f.getBoolean(x)); 

  }
}

The code above generates the following result.