简体   繁体   中英

Members of Java array class

Reflexion API shows that any Java array class implements interface java.lang.Cloneable and java.io.Serializable. It does not have any member declared.

My questions are:

  • Where this 'length' is defined?

  • Where the protected Object clone() is overridden with public access specifier using co-variant return type(byte[] replacing Object) as we can directly assign it to a byte[]?

  • Where the association(IS-A) with Cloneable and Serializable is defined?

Also access specifier for the byte[] class contains "abstract final" which is not a legal combination of any class or method in Java.

    import java.lang.reflect.*;

    public class ArrayExplorer {

      public static void main(String[] args) {
      explore("Current class:", byte[].class);

      byte[] bytes = { 65, 'A' };
      System.out.println(bytes.length);

      byte[] cloned = bytes.clone();
      System.out.println(cloned);

   }

private static void explore(String msg, Class<?> class1) {
    if (class1 == null)
        return;

    System.out.println("**************************************\n" + msg
            + Modifier.toString(class1.getModifiers()) + " " + class1);

    // if (class1 == Object.class)
    // return;
    Field[] fields = class1.getDeclaredFields();
    for (Field field : fields) {
        System.out.println(field);
    }

    Method[] methods = class1.getDeclaredMethods();
    for (Method method : methods) {
        System.out.println(method);
    }

    explore("Superclass:", class1.getSuperclass());
    explore("Classes:", class1.getClasses());
    explore("ComponentType:", class1.getComponentType());
    explore("DeclaredClasses:", class1.getDeclaredClasses());
    explore("DeclaringClass:", class1.getDeclaringClass());
    explore("EnclosingClass:", class1.getEnclosingClass());

    if (!class1.isInterface()) {
        explore("Interfaces:", class1.getInterfaces());
    }

}

private static void explore(String msg, Class<?>[] classes) {
    if (classes == null || classes.length == 0)
        return;
    System.out.println(msg);
    for (Class<?> class1 : classes) {
        explore("", class1);
    }
    }
  }

java中的数组有点像基元:虽然有一个Array.class来表示它,但它并没有真正的“代码”,因为类型是直接构建在语言中的,因此它的字段如length等等。通过反思可以访问。

JavaDoc

getDeclaredMethods

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.

getDeclaredMethods

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM