public class Person { public int age; public static int weight; }
Person person = new Person(); try { // Non static variable with object Person.class.getField("age").setInt(person, 15); // Setting static variable Person.class.getField("weight").setInt(null, 3); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(person.age); System.out.println(person.weight);
15 3