Java Field.setInt(Object obj, int i)

Syntax

Field.setInt(Object obj, int i) has the following syntax.

public void setInt(Object obj,  int i)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.setInt(Object obj, int i) method.


import java.awt.Rectangle;
import java.lang.reflect.Field;
// w w w. jav  a2 s .  c o m
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");
      heightField.setInt(r, 1000);
      Integer heightValue = (Integer) heightField.get(r);
      System.out.println("Height: " + heightValue.toString());
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable