Field get() method in Java with Examples

26 Aug, 2019
The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an instance field. This method throws a NullPointerException if the specified obj argument is null and an IllegalArgumentException If the specified object is not an instance of the class or interface declaring the underlying field. If the field is hidden in the type of obj, the field’s value is obtained according to the preceding rules.
Syntax:
public double get(Object obj)
             throws IllegalArgumentException,
                    IllegalAccessException

Parameters: This method accepts a single parameter obj which is the object to extract the field value from.
Return value: This method returns the value of the represented field in object obj; primitive values are wrapped in an appropriate object before being returned.
Exception: This method throws following Exception:
IllegalAccessException– if this Field object is enforcing Java language access control and the underlying field is inaccessible.IllegalArgumentException– if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).NullPointerException– if the specified object is null and the field is an instance field.ExceptionInInitializerError– if the initialization provoked by this method fails.
IllegalAccessException– if this Field object is enforcing Java language access control and the underlying field is inaccessible.
IllegalArgumentException– if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
NullPointerException– if the specified object is null and the field is an instance field.
ExceptionInInitializerError– if the initialization provoked by this method fails.
Below programs illustrate get() method:Program 1:
// Java program to demonstrate get() method  import java.lang.reflect.Field;  public class GFG {      public static void main(String[] args)        throws Exception    {          // Create the User class object        User user = new User();          // Get the all field objects of User class        Field[] fields = User.class.getFields();          for (int i = 0; i < fields.length; i++) {              // get value of the fields            Object value = fields[i].get(user);              // print result            System.out.println("Value of Field "                               + fields[i].getName()                               + " is " + value);        }    }}  // sample User classclass User {      // static double values    public static double Marks = 34.13;    public static int Fees = 34199;    public static String name = "Aman";      public static double getMarks()    {        return Marks;    }      public static void setMarks(double marks)    {        Marks = marks;    }      public static int getFees()    {        return Fees;    }      public static void setFees(int fees)    {        Fees = fees;    }      public static String getName()    {        return name;    }      public static void setName(String name)    {        User.name = name;    }}
Value of Field Marks is 34.13
Value of Field Fees is 34199
Value of Field name is Aman

Program 2:
// Java program to demonstrate get() method  import java.lang.reflect.Field;  public class GFG {      public static void main(String[] args)        throws Exception    {          // Create the RealNumbers class object        Fields field = new Fields();          // Get the all field objects of User class        Field[] fieldsOfFieldClass            = Fields.class.getFields();          for (int i = 0;             i < fieldsOfFieldClass.length; i++) {              // get value            Object value = fieldsOfFieldClass[i]                               .get(field);              // print result            System.out.println(                "Value of Field "                + fieldsOfFieldClass[i].getName()                + " is " + value);        }    }      // RealNumbers class    static class Fields {          // double field        public static final double doubleValue            = 9999999.34567;        public static final int intValue            = 9999999;        public static final float floatValue            = 9999999.34567f;        public static final            boolean booleanValue            = true;    }}
Value of Field doubleValue is 9999999.34567
Value of Field intValue is 9999999
Value of Field floatValue is 9999999.0
Value of Field booleanValue is true

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#get-java.lang.Object-
Java-Field
Java-Functions
java-lang-reflect-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
HashMap in Java with Examples
ArrayList in Java
Multidimensional Arrays in Java
Collections in Java
Set in Java
Stream In Java
Initializing a List in Java
Singleton Class in Java