Java Reflection Java Java API
Class:
java.lang.reflect.Field
Method:
public void set(Object obj,
Object value)
throws IllegalArgumentException,
IllegalAccessException
Sets this field value for the provided object.
Examples
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class SetExample { private String myStr;
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field f = SetExample.class.getDeclaredField("myStr"); SetExample instance = new SetExample(); f.set(instance, "test string"); System.out.println(f.get(instance)); } }
Outputtest string
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class SetExample2 { private static String myStaticStr;
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field f = SetExample2.class.getDeclaredField("myStaticStr"); f.set(null, "a test static string"); System.out.println(f.get(null)); } }
Outputa test static string
|
|