Java Reflection 3 Class' a Ait Constructor 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;
  }
}

 

Constructor<?>[] cons = Person.class.getConstructors();
for(Constructor c:cons) {
  System.out.print((Modifier.toString(c.getModifiers())+" "+ c.getName()));
  Parameter[] parameters = c.getParameters();
  System.out.print("(");
  for(Parameter param : parameters) {
    System.out.print(param.getType()+" "+param.getName()+",");
  }
  System.out.println(")");
}
public com.dogukan.b1.Person(class java.lang.String arg0,class java.lang.String arg1,int arg2,)
public com.dogukan.b1.Person()