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);
}
}
}
JavaDoc
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.
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.