Open In App

Field setChar() method in Java with Examples

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:



Return: This method returns nothing. 

Exception: This method throws the following Exception:



Below programs illustrate setChar() method: 

Program 1: 




// 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 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-


Article Tags :