class Test {
public static void main(String[] args) throws Exception {
Test t = new Test();
System.out.println(t.field);
System.out.println(t.getClass().getField("field").get(t));
int[] ar = new int[23];
System.out.println(ar.length);
System.out.println(ar.getClass().getField("length").get(ar));
}
public int field = 10;
};
当我运行上面的代码时,我在命令行上得到以下结果——
10
10
23
Exception in thread "main" java.lang.NoSuchFieldException: length
at java.lang.Class.getField(Class.java:1520)
at Test.main(Test.java:9)
为什么我无法访问数组中的“长度”字段?