Gets the value of field on the given instance via reflection - Java Reflection

Java examples for Reflection:Field Value

Description

Gets the value of field on the given instance via reflection

Demo Code


import javax.ws.rs.core.MultivaluedMap;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main{
    /**/*from   w  ww. java  2  s. c o  m*/
         * Gets the value of field on the given instance
         * @param field the field
         * @param instance the object instance
         * @return the value
         */
        public static Object getFieldValue(Field field, Object instance) {
            try {
                field.setAccessible(true);
                return field.get(instance);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
}

Related Tutorials