Close

Java Reflection - Field.setInt() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

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

Sets the value of a field as an int on the specified object.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class SetIntExample {
private int myInt;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = SetIntExample.class.getDeclaredField("myInt");
SetIntExample instance = new SetIntExample();
f.setInt(instance, 5);
System.out.println(f.getInt(instance));
}
}

Output

5




See Also