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.
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.
Below programs illustrate get() method:
Value of Field Marks is 34.13 Value of Field Fees is 34199 Value of Field name is Aman
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