当前位置: 代码迷 >> J2SE >> 反射有关问题
  详细解决方案

反射有关问题

热度:120   发布时间:2016-04-24 01:52:25.0
反射问题
Java code
class Test11 {    static final int staticFinal = 11;    void show() {        System.out.println(this.getClass().getCanonicalName());    }}public class Temp {    public static void main(String[] args) {        Test11 test = new Test11();        test.show();                Class test1=null;        try {            test1=Class.forName("a10_2.Test11");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        [color=#FF0000]test1????[/color]    }}


为什么看书上在红色的 test1那里可以访问staticfinal 变量,而在我这不行呢 ,百思不得其解啊,求教了

------解决方案--------------------
你要确保a10_2文件目录中有一个Test11.class
------解决方案--------------------
在顶上加上
Java code
package a10_2;
------解决方案--------------------
http://happyran.zbpifa.com
http://007ej.com/user.asp
------解决方案--------------------
这问题和反射有关吗
------解决方案--------------------
Class test1=null;
try {
test1=Class.forName("a10_2.Test11");//这里反射
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果你在这里直接通过test1访问成员是访问不到了,因为没有具体实例对象,也就没有实例成员
------解决方案--------------------
用test1.getDeclaredField("staticFinal ").getInt();应该可以得到吧,可以试试...
  相关解决方案