setChar() method of java.lang.reflect.Field used to set the value of a field as a char on the specified object. When you need to set the value of a field of an object as char then you can use this method to set value over an Object. Syntax:
public void setChar(Object obj, char c) throws IllegalArgumentException, IllegalAccessException
Parameters: This method accepts  two parameters:
- obj: which is the object whose field should be modified and
- c: which is the new value for the field of obj being modified.
Return: This method returns nothing.Â
Exception: This method throws the following Exception:
- IllegalAccessException: if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
- IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementer thereof), or if an unwrapping conversion fails.
- NullPointerException: if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: if the initialization provoked by this method fails.
Below programs illustrate setChar() method:Â
Program 1:Â
Java
// Java program to illustrate setByte() method Â
// program illustrate setChar() import java.lang.reflect.Field; Â
public class GFG { Â
    public static void main(String[] args)         throws Exception     { Â
        // create user object         Employee emp = new Employee(); Â
        // print value of lastNamePrefix         System.out.println(             "Value of lastNamePrefix before "             + "applying setChar is "             + emp.lastNamePrefix); Â
        // Get the marks field object         Field field = Employee. class                           .getField("lastNamePrefix"); Â
        // Apply setChar Method         field.setChar(emp, 'B' ); Â
        // print value of lastNamePrefix         System.out.println(             "Value of lastNamePrefix after "             + "applying setChar is "             + emp.lastNamePrefix); Â
        // print value of firstNamePrefix         System.out.println(             "Value of firstNamePrefix before "             + "applying setChar is "             + emp.firstNamePrefix); Â
        // Get the marks field object         field = Employee. class                     .getField("firstNamePrefix"); Â
        // Apply setChar Method         field.setChar(emp, 'Z' ); Â
        // print value of firstNamePrefix         System.out.println(             "Value of firstNamePrefix after "             + "applying setChar is "             + emp.firstNamePrefix);     } } Â
// sample class class Employee { Â
    // static char values     public static char firstNamePrefix = 'A' ;     public static char lastNamePrefix = 'S' ; } |
Value of lastNamePrefix before applying setChar is S Value of lastNamePrefix after applying setChar is B Value of firstNamePrefix before applying setChar is A Value of firstNamePrefix after applying setChar is Z
Program 2:Â
Java
// Java program to illustrate setChar() method Â
import java.lang.reflect.Field; Â
public class GFG { Â
    public static void main(String[] args)         throws Exception     { Â
        // create user object         User user = new User(); Â
        // Get the id field object         Field field = User. class                           .getField("id"); Â
        // Apply setChar Method         field.setChar(user, 'A' ); Â
        // print value of isActive         System.out.println("Value after "                            + "applying setChar is "                            + user.id);     } } Â
// sample User class class User { Â
    // static char values     public static char id = 'Z' ; } |
Value after applying setChar is A