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.


/*  ww  w.  j a  v  a 2s  . c o m*/
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.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable