Java Reflection 4 Class Degiskenini Degistirmek

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

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *