Field get() method in Java with Examples
Last Updated :
09 Jan, 2023
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 implementer 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
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
User user = new User();
Field[] fields = User. class .getFields();
for ( int i = 0 ; i < fields.length; i++) {
Object value = fields[i].get(user);
System.out.println("Value of Field "
+ fields[i].getName()
+ " is " + value);
}
}
}
class User {
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
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
Fields field = new Fields();
Field[] fieldsOfFieldClass
= Fields. class .getFields();
for ( int i = 0 ;
i < fieldsOfFieldClass.length; i++) {
Object value = fieldsOfFieldClass[i]
.get(field);
System.out.println(
"Value of Field "
+ fieldsOfFieldClass[i].getName()
+ " is " + value);
}
}
static class Fields {
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-
Share your thoughts in the comments
Please Login to comment...