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.

  • bcMember:
  • member identifier
  • identifer of class that contains this member
  • boolean: true if member is static, false otherwise
  • boolean: true if member is a field; false otherwise
  • qualified name of member's (return) type
  • array qualifier: ' ' means return type is not an array, '[ ]' is a 1D array, '[ ][ ]' is a 2D array, and so on.
  • signature of member: for a field, it is the name of the field. For a method, it is the name of the method followed by a list of its parameters.
  • 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

    上一篇: class.isAssignableFrom()当不是相同的包时发送false

    下一篇: 如何找出类成员是静态的还是Java中的字段