Mega Code Archive

 
Categories / Java Tutorial / Reflection
 

Getting and Setting the Value of a Field (assumes that the field has the type int)

import java.lang.reflect.Field; public class Main {   public static void main(String[] argv) throws Exception {     Class cls = java.awt.Point.class;     Field field = cls.getField("x");     // Get value //    field.getInt(object);     // Set value   //  field.setInt(object, 123);     // Get value of a static field     field.getInt(null);     // Set value of a static field     field.setInt(null, 123);   } }