How to find out if a class member is static and a field in Java
How do you find out whether a member of a class is static and a field? I tried using the .getModifiers() method but it didn't return the desired result.
The desired output for the yparser.connection package is:
bcClass(c0,'yparser.connection','Object').
/* public Constructors */
bcMember(m0,c0,true,false,'yparser.connection','','connection(String,String,String,String,String,String)').
/* public Fields */
bcMember(m1,c0,true,true,'String','','quote').
bcMember(m2,c0,true,true,'String','','comma').
bcMember(m3,c0,false,true,'String','','name1').
bcMember(m4,c0,false,true,'String','','role1').
bcMember(m5,c0,false,true,'String','','end1').
bcMember(m6,c0,false,true,'String','','name2').
bcMember(m7,c0,false,true,'String','','role2').
bcMember(m8,c0,false,true,'String','','end2').
/* public Methods */
bcMember(m9,c0,true,false,'void','','dump()').
You know it's a field because it's a Field
object.
To determine if it's static:
if(Modifier.isStatic(f.getModifiers()))
System.out.println("Field is static!");
or
if((f.getModifiers() & Modifier.STATIC) != 0)
System.out.println("Field is static!");
fields[] fld= TheClass.class.getDeclaredFields();
for (Field fldd : fld) {
if (java.lang.reflect.Modifier.isStatic(fldd.getModifiers())) {
//Then the fldd is static
}
}
链接地址: http://www.djcxy.com/p/57410.html