我在一个类中声明了 几个静态成员,然后我在其他类中利用 反射机制,找到了
该Field (即对应静态成员)
但是,能否得到它的值呢?如何取得比如
class A

    public static final String value = "JLKJDFSJDFKLJSD:F";}我已经找到 value这个field,但如何取得这个"JLKJDFSJDFKLJSD:F" String(注,不是用jni在C中得到)
谢了先

解决方案 »

  1.   

    Field f=....;//得到这个filed
    String value=(String)f.get(null);
      

  2.   

    你可以将String value = "JLKJDFSJDFKLJSD:F";放在接口中
    例如class interface I1
    {
       String value = "JLKJDFSJDFKLJSD:F";}
    然后用一个普通类去继承它,就可以获取它里面定义的了!
    例如public  class getValue extends I1
       {
            }
      

  3.   

    谢谢楼上二位
     congbailing_914(奇迹玩家) ( )
    不好意思,你的方法不适何我用,谢谢你
     jFresH_MaN(The answer is ......)(上帝对我说:编程要全面发展,) ( ) 信誉:123 
    Field f=....;//得到这个filed
    String value=(String)f.get(null);
    是正解,尤其要谢谢 jFresH_MaN(The answer is ......)(上帝对我说:编程要全面发展,) ( ) 信誉:123 
    经常看到它为许多人解惑,
    支持 当  盟主!
      
      

  4.   

    支持 当  盟主!
    唉,我也是这段话都没有看清楚Returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type. The underlying field's value is obtained as follows: If the underlying field is a static field, 
    the obj argument is ignored; it may be null. Otherwise, the underlying field is an instance field. If the specified obj argument is null, the method throws a NullPointerException. If the specified object is not an instance of the class or interface declaring the underlying field, the method throws an IllegalArgumentException. If this Field object enforces Java language access control, and the underlying field is inaccessible, the method throws an IllegalAccessException. If the underlying field is static, the class that declared the field is initialized if it has not already been initialized. Otherwise, the value is retrieved from the underlying instance or static field. If the field has a primitive type, the value is wrapped in an object before being returned, otherwise it is returned as is. If the field is hidden in the type of obj, the field's value is obtained according to the preceding rules. 
    If the underlying field is a static field, 
    the obj argument is ignored; it may be null. 
    我还传了半天也不知道错在哪里!汗颜阿
      

  5.   

    不好意思 上面的代码写错了!
    我给你个刚写的例子:你参考参考
    public class getValue implements I1
    {
    public static void main(String [] args)
    {

    System.out.println(value);

    }
    }
     interface I1
    {
    String value="fafasdfsdfasf:F";
    }
      

  6.   

    jFresH_MaN(The answer is ......)(上帝对我说:编程要全面发展,) 我下面的那个程序不对吗??不是也可以达到楼主的目的啊!
      

  7.   

    楼主是想用反射机制来读取一个类的static成员的值
      

  8.   

    谢谢!各位支持!明天结贴!RondyBin(冰) ( ) 信誉:100 说话不必如此刻薄,各人擅长不一样嘛,有的是喜欢写 jsp/servlet /ejb
    而有的又是写 Awt/swing的
    还有的搞XML相关技术的恐怕很难所有的都很熟悉.
    我是从c转过来的,所以我jsp/servlet /ejb 几乎一窍不通,但是也许我的汇编比人家的好,就是这样啊不管怎样 谢谢大家!
      

  9.   

    看来搂主没有好好看jdk document啊。
      

  10.   

    public class teststatic
    {
        public static final teststatic XYZ = new teststatic();
    }import java.lang.reflect.* ;public class classRef
    {    public static void main ( String[] args ) throws SecurityException , NoSuchFieldException , IllegalAccessException , IllegalArgumentException
        {
            Class c = teststatic.class;
            Field f = c.getDeclaredField("value");
            String str = f.get(null).toString();
            System.out.println ( str ) ;
        }
    }
      

  11.   

    public class teststatic
    {
          public static final String value = "JLKJDFSJDFKLJSD:F";
    }
    import java.lang.reflect.* ;
    public class classRef
    {public static void main ( String[] args ) throws SecurityException , NoSuchFieldException , IllegalAccessException , IllegalArgumentException
        {
            Class c = teststatic.class;
            Field f = c.getDeclaredField("value");
            String str = f.get(null).toString();
            System.out.println ( str ) ;
        }
    }