Close

Java Reflection - Field.getChar() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public char getChar(Object obj)
             throws IllegalArgumentException,
                    IllegalAccessException

Gets the value of a static or instance field of type char. The provided 'obj' is the object to extract the boolean value from (should be null for static fields).


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetCharExample {
private char aChar = 'a';
private static char aStaticChar = 'z';

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field field = GetCharExample.class.getDeclaredField("aChar");
char aChar = field.getChar(new GetCharExample());
System.out.println(aChar);

Field field2 = GetCharExample.class.getDeclaredField("aStaticChar");
char aChar2 = field2.getChar(null);
System.out.println(aChar2);
}
}

Output

a
z




See Also