Use reflection to get the variable name of the class, give you an example
public class MyTools {
public static void Setallcomponentsname (Object f) {
Gets all domains in the corresponding class of the F object
field[] fields = F.getclass (). Getdeclaredfields ();
for (int i = 0, len = fields.length; i < Len; i++) {
For each property, get the property name
String varName = Fields[i].getname ();
try {
Get the original access control permissions
Boolean accessflag = Fields[i].isaccessible ();
Modify access Control permissions
Fields[i].setaccessible (TRUE);
Gets the variable in object F of the property fields[i] corresponding to
Object o = Fields[i].get (f);
System.out.println ("The incoming object contains a variable as follows:" + varName + "=" + O);
Restore access control permissions
Fields[i].setaccessible (Accessflag);
} catch (IllegalArgumentException ex) {
Ex.printstacktrace ();
} catch (Illegalaccessexception ex) {
Ex.printstacktrace ();
}
}
}
Use reflection to get the variable name of the class Java