The getChar() method of java.lang.reflect.Field is used to get the value of char which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type char via a widening conversion. When a class contains a static or instance Char field and we want to get the value of that field then we can use this method to return the value of Field.
Syntax:
public char getChar(Object obj)
       throws IllegalArgumentException,
              IllegalAccessException
Parameters: This method accepts a single parameter obj which is the object to extract the char value from.
Return value: This method returns the value of field converted to type char.
Exception: This method throws following Exception:
- IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
- IllegalArgumentException: This exception is thrown if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type char by a widening conversion.
- NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: This exception is thrown if the initialization provoked by this method fails.
Below programs illustrate getChar() method:
Program 1:
| // Java program to demonstrate the above method Âimportjava.lang.reflect.Field; ÂpublicclassGFG { Â    publicstaticvoidmain(String[] args)        throwsNoSuchFieldException,               SecurityException,               IllegalArgumentException,               IllegalAccessException    { Â        // Create the User class object        User user = newUser(); Â        // Get the identificationChar field object        Field field            = User.class.getField("identificationChar"); Â        // Apply getChar Method on User Object        // to get the value of identificationChar field        charvalue = field.getChar(user); Â        // print result        System.out.println("Value of Char Field"                           + " identificationChar is "                           + value); Â        // Now Get the selectionChar field object        field = User.class.getField("selectionChar"); Â        // Apply getChar Method on User Object        // to get the value of selectionChar field        value = field.getChar(user); Â        // print result        System.out.println("Value of Char Field"                           + " selectionChar is "                           + value);    }} Â// sample User classclassUser { Â    // static char values    publicstaticcharidentificationChar = 'E';    publicstaticcharselectionChar = 'A';    publicstaticString name = "Aman"; Â    // getter and setter methods    publicstaticchargetIdentificationChar()    {        returnidentificationChar;    } Â    publicstaticvoid    setIdentificationChar(charidentificationChar)    {        User.identificationChar = identificationChar;    } Â    publicstaticchargetSelectionChar()    {        returnselectionChar;    } Â    publicstaticvoid    setSelectionChar(charselectionChar)    {        User.selectionChar = selectionChar;    } Â    publicstaticString getName()    {        returnname;    } Â    publicstaticvoidsetName(String name)    {        User.name = name;    }} | 
Value of Char Field identificationChar is E Value of Char Field selectionChar is A
Program 2:
| // Java program to demonstrate the above method Âimportjava.lang.reflect.Field; ÂpublicclassGFG { Â    publicstaticvoidmain(String[] args)        throwsNoSuchFieldException,               SecurityException,               IllegalArgumentException,               IllegalAccessException    { Â        // Create the Alphabets class object        Alphabets alphabet = newAlphabets(); Â        // Get the value field object        Field field            = Alphabets.class.getField("value"); Â        // Apply getChar Method on field Object        // to get the value of value field        charvalue = field.getChar(alphabet); Â        // print result        System.out.println("Value: "+ value);    } Â    // Alphabets class    staticclassAlphabets { Â        // char field        publicstaticcharvalue = 'Q'; Â        // getter and setter methods        publicstaticchargetValue()        {            returnvalue;        } Â        publicstaticvoidsetValue(charvalue)        {            Alphabets.value = value;        }    }} | 
Value: Q
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getChar-java.lang.Object-

 
                                    







