Fields of the Java reflection Class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.