PackageCom.whbs.bean;  Public classUserBean {PrivateInteger ID; Private intAge ; PrivateString name; PrivateString address;  PublicUserBean () {System.out.println ("Instantiation"); }            PublicInteger getId () {returnID; }       Public voidsetId (Integer id) { This. ID =ID; }       Public intGetage () {returnAge ; }       Public voidSetage (intAge ) {          This. Age =Age ; }       PublicString GetName () {returnname; }       Public voidsetName (String name) { This. Name =name; }       PublicString getaddress () {returnaddress; }       Public voidsetaddress (String address) { This. Address =address; }                 }     2 >Reflection Test Packagecom.whbs.test; ImportJava.lang.reflect.Field; ImportJava.lang.reflect.Method; ImportCom.whbs.bean.UserBean;  Public classTest1 { Public Static voidMain (string[] args)throwsException {/** Method of listing Classes 1*/         //String ClassPath = "Com.whbs.bean.UserBean"; //Class cla = Test1.class.getClassLoader (). LoadClass (ClassPath); //Object ob = Cla.newinstance ();                /** Method of listing Classes 2*/UserBean Bean=NewUserBean (); Bean.setid (100); Bean.setaddress (Wuhan); //Get Class objectClass USERCLA =(Class) Bean.getclass (); /** Get all the property collections in the class*/field[] FS=Usercla.getdeclaredfields ();  for(inti = 0; i < fs.length; i++) {Field F=Fs[i]; F.setaccessible (true);//set some properties to be accessibleObject val = f.get (Bean);//get the value of this propertySystem.out.println ("Name:" +f.getname () + "\ t value =" +val); String type= F.gettype (). toString ();//get the type of this property           if(Type.endswith ("String") {System.out.println (F.gettype () )+ "\ T is string"); F.set (Bean,"12");//set a value for a property}Else if(Type.endswith ("int") | | type.endswith ("Integer")) {System.out.println (F.gettype () )+ "\ t is int"); F.set (Bean,12);//set a value for a property}Else{System.out.println (F.gettype ()+ "\ T"); }                     }                         /** Get the method in the class*/method[] Methods=Usercla.getmethods ();  for(inti = 0; i < methods.length; i++) {Method method=Methods[i]; if(Method.getname (). StartsWith ("Get") {System.out.print ("MethodName:" +method.getname () + "\ T"); System.out.println ("Value:" +method.invoke (bean));//get the value of the Get method           }         }      }     }  
Java reflection Gets the value of the property and sets the value of the property