Field setChar() method in Java with Examples Last Updated : 10 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 andc: 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'; } Output: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'; } Output:Value after applying setChar is A Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setChar-java.lang.Object-char- Comment More infoAdvertise with us Next Article Reader read() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java-lang-reflect-package Java-Field Practice Tags : Java Similar Reads Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol 3 min read Field toGenericString() method in Java with Examples The toGenericString() method of java.lang.reflect.Field is used to return a string which represents this Field, including its generic type. The format of the string is the access modifiers for the field, if any, followed by the generic field type, followed by a space, followed by the fully-qualified 2 min read FieldPosition toString() method in Java with Example The toString() method of java.text.FieldPosition class is used to represent the field position object in the form of string. Syntax: public String toString() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the string representation of this FieldPo 2 min read Reader read() method in Java with Examples The read() method of Reader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It 3 min read Reader read() method in Java with Examples The read() method of Reader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It 3 min read Modifiers fieldModifiers() method in Java with Examples The fieldModifiers() method of java.lang.reflect.Modifier class is used to get an integer value together with the modifiers of source language that can be applied to a field. Syntax: public static boolean fieldModifiers() Parameters: This method accepts nothing. Return: This method returns an int va 1 min read Like