Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaField get() method in Java with Examples

Field get() method in Java with Examples

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:

  1. IllegalAccessException– if this Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. IllegalArgumentException– if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementer thereof).
  3. NullPointerException– if the specified object is null and the field is an instance field.
  4. ExceptionInInitializerError– if the initialization provoked by this method fails.

Below programs illustrate get() method: Program 1: 

Java




// 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 class
class 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;
    }
}


Output:

Value of Field Marks is 34.13
Value of Field Fees is 34199
Value of Field name is Aman

Program 2: 

Java




// 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;
    }
}


Output:

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-

RELATED ARTICLES

Most Popular

Recent Comments