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


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

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


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");
    f.setChar(x, 'a');


The code above generates the following result.