Open In App

Field getChar() method in Java with Examples

Last Updated : 26 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. 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.
  3. NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
  4. 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
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the User class object
        User user = new User();
  
        // Get the identificationChar field object
        Field field
            = User.class.getField("identificationChar");
  
        // Apply getChar Method on User Object
        // to get the value of identificationChar field
        char value = 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 class
class User {
  
    // static char values
    public static char identificationChar = 'E';
    public static char selectionChar = 'A';
    public static String name = "Aman";
  
    // getter and setter methods
    public static char getIdentificationChar()
    {
        return identificationChar;
    }
  
    public static void
    setIdentificationChar(char identificationChar)
    {
        User.identificationChar = identificationChar;
    }
  
    public static char getSelectionChar()
    {
        return selectionChar;
    }
  
    public static void
    setSelectionChar(char selectionChar)
    {
        User.selectionChar = selectionChar;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}


Output:

Value of Char Field identificationChar is E
Value of Char Field selectionChar is A

Program 2:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the Alphabets class object
        Alphabets alphabet = new Alphabets();
  
        // Get the value field object
        Field field
            = Alphabets.class.getField("value");
  
        // Apply getChar Method on field Object
        // to get the value of value field
        char value = field.getChar(alphabet);
  
        // print result
        System.out.println("Value: " + value);
    }
  
    // Alphabets class
    static class Alphabets {
  
        // char field
        public static char value = 'Q';
  
        // getter and setter methods
        public static char getValue()
        {
            return value;
        }
  
        public static void setValue(char value)
        {
            Alphabets.value = value;
        }
    }
}


Output:

Value: Q

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getChar-java.lang.Object-



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads