The class Person and Student have same fields name of type String and age of Type Integer. Therefore, we can set a student’s fields using a person’s counterpart fields:
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.reflection;
publicclassMain{ publicstaticvoidmain(String[] args){ Person person = new Person(); person.setName("Eric"); person.setAge(24);
Student student = new Student(); student.setName(person.getName()); student.setAge(person.getAge()); } }
publicclassMain{ publicstaticvoidmain(String[] args)throws InvocationTargetException, IllegalAccessException { Person person = new Person(); person.setName("Eric"); person.setAge(24);