With reflection, you can get all declared member variables (including all), with the following code:
Package Spt.test.src;public class Person {private String name = "Pri_name";p ublic String ID;}
To run the test:
Package Spt.test;import Java.lang.reflect.field;import spt.test.src.person;class reflectiontest {@SuppressWarnings ( "Unchecked") public static void main (string[] args) throws illegalaccessexception {person person * = new person ();p erson.id = "Akk"; Class<person> PClass = (class<person>) person.getclass (); for (Field Field:pClass.getDeclaredFields ()) { try {//set accessable.field.setAccessible (true);//get field name and value. System.out.println (Field.getname () + ":" + field.get (person));} catch (IllegalArgumentException e) {e.printstacktrace ();}}}}
will be output on the console:
Name:pri_nameid:akk
Java reflection-Gets the value of the member variable.