Java Field.get(Object obj)

Syntax

Field.get(Object obj) has the following syntax.

public Object get(Object obj)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.get(Object obj) method.


import java.awt.Rectangle;
import java.lang.reflect.Field;
// w w w .  ja  v a  2 s  .  c om
public class Main {

  public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();
    try {
      Field heightField = c.getField("height");
      Integer heightValue = (Integer) heightField.get(r);
      System.out.println("Height: " + heightValue.toString());
    } catch (NoSuchFieldException e) {
      System.out.println(e);
    } catch (SecurityException e) {
      System.out.println(e);
    } catch (IllegalAccessException e) {
      System.out.println(e);
    }
  }
}

The output:





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable