Java Reflection 1 Class Değişkenlerini Listelemek

Posted by dogukanhan on September 04, 2018 · 1 min read
public class Person {
  public final static int ID = 0;
  private String name,surname;
  private int age;
  private static List<Person> persons = new ArrayList<>();
  
  public Person() {}
  
  public Person(String name,String surname,int age) {
    this.name=name;
    this.surname=surname;
    this.age = age;
  }
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSurname() {
    return surname;
  }
  public void setSurname(String surname) {
    this.surname = surname;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
  public static void addToList(Person person) {
    persons.add(person);
  }
  
  public static List<Person> getPersons() {
    return persons;
  }
}
public class Main {
  public static void main(String args[]) {
    Field[] fields = Person.class.getDeclaredFields();
    for(Field field : fields) {
      System.out.println(Modifier.toString(field.getModifiers())+" "+field.getType()+","+field.getName());
    }
  }
}
public static final int,ID
private class java.lang.String,name
private class java.lang.String,surname
private int,age
private static interface java.util.List,persons