java反射類成員變量 – JAVA編程語言程序開發技術文章

代碼如下:
[java] 
package com.test; 
 
import java.lang.reflect.Field; 
import java.lang.reflect.Modifier; 
 
public class Test_5 { 
     
    public static final int a = 111; 
    public static final int b = 222; 
    public static final int c = 333; 
    public static final int d = 444; 
     
    public static void main(String[] args) { 
        try { 
            Class c = Class.forName("com.test.Test_5"); 
            Field[] fields = c.getDeclaredFields(); 
            for (int i = 0; i < fields.length; i++) { 
                String m = Modifier.toString(fields[i].getModifiers()); 
                if (m != null && m.indexOf("final") > -1) { 
                    System.out.println(fields[i].getInt(int.class)); 
                } 
            } 
        } catch (ClassNotFoundException e) { 
            e.printStackTrace(); 
        } catch (IllegalArgumentException e) { 
            e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
            e.printStackTrace(); 
        } 
         
    } 

發佈留言