Fields of the Java Reflection class:
Package Com.ma.reflection;import Java.lang.reflect.field;import Org.junit.test;import com.ma.bean.StudentBean;/** * Reflection field * @author Ma * */public class Demo4 {/** * reflection public field * @throws classnotfoundexception * @throws nosuchfieldexception * @throws SecurityException * @throws illegalaccessexception * @throws illegalargumentexception */@Testpublic void test1 () throws ClassNotFoundException, SecurityException, Nosuchfieldexception, IllegalArgumentException, illegalaccessexception{//Load class class<?> clazz = Class.forName ("Com.ma.bean.StudentBean"); Studentbean s = new Studentbean ();//load fields Field field = Clazz.getfield ("name");//Call field String name = (string) field.get (new St Udentbean ());//Set field value Field.set (S, (Object) "close");//Output field System.out.println (S.name); SYSTEM.OUT.PRINTLN (name);} /** * Reflection Private field * @throws classnotfoundexception * @throws nosuchfieldexception * @throws SecurityException * @throws Ille Galaccessexception * @throws illegalargumentexception */@Testpublic void Test2 () throws ClassnotFoundexception, SecurityException, Nosuchfieldexception, IllegalArgumentException, IllegalAccessException{// Load class class<?> clazz = Class.forName ("Com.ma.bean.StudentBean"); Studentbean s = new Studentbean ();//load fields Field field = Clazz.getdeclaredfield ("work"); Field.setaccessible (true);// Gets the field String work = (string) field.get (s); SYSTEM.OUT.PRINTLN (work);} /** * Reflection static field * @throws ClassNotFoundException * @throws nosuchfieldexception * @throws SecurityException * @throws Ille Galaccessexception * @throws illegalargumentexception */@Testpublic void Test3 () throws ClassNotFoundException, Securit Yexception, Nosuchfieldexception, illegalargumentexception, illegalaccessexception{////load class class<?> clazz = Class.forName ("Com.ma.bean.StudentBean"); Studentbean s = new Studentbean ();//load fields Field field = Clazz.getdeclaredfield ("Sex"); Field.setaccessible (true);// Gets the field String sex = (String) field.get (s); SYSTEM.OUT.PRINTLN (sex);} /** * Reflection field */@Testpublic void test4 () {//////}/** * reflection field */@Testpublic voidTest5 () {//////}}
Entity class Student
Package com.ma.bean;/** * Student class * @author MA * */public class Studentbean {public String name = "Liu Bei";p ublic int age;private String work = "Teacher";p rivate static String sex = "male";p ublic Studentbean () {super ();} public void Eat () {System.out.println ("Eat up ..."). ");} public void Play (String name,int age) {System.out.println (name+): Go play yo ... "+age);} public void run (int ... intaa) {System.out.println (": This is a mutable parameter method");} private void Jump () {System.out.println ("This is Private Method");} public static void sleep (int age) {System.out.println (age+ ": This is a static method");} public static void Main (string[] args) {System.out.println ("This is the Main method!!! ");}}
Fields of the Java reflection Class