What is Class Descriptor?
Yes, a Class
object is a class descriptor for a certain "class".
From the API:
Instances of this class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as
Class
objects.
Here's an example of a simple usage of Class
methods to reflectively describe types:
static void describe(Class<?> clazz, String pad, String leadin) {
if (clazz == null) return;
String type =
clazz.isInterface() ? "interface" :
clazz.isArray() ? "array" :
clazz.isPrimitive() ? "primitive" :
clazz.isEnum() ? "enum" :
"class";
System.out.printf("%s%s%s %s ( %s )%n",
pad, leadin, type, clazz.getSimpleName(), clazz.getName());
for (Class<?> interfaze : clazz.getInterfaces()) {
describe(interfaze, pad + " ", "implements ");
}
describe(clazz.getComponentType(), pad + " ", "elements are ");
describe(clazz.getSuperclass(), pad + " ", "extends ");
}
static void describe(Class<?> clazz) {
describe(clazz, "", "");
System.out.println();
}
public static void main(String[] args) {
describe(boolean[][].class);
describe(java.math.RoundingMode.class);
describe(java.util.ArrayList.class);
describe(void.class);
}
The above snippet produces the following output:
array boolean[][] ( [[Z )
implements interface Cloneable ( java.lang.Cloneable )
implements interface Serializable ( java.io.Serializable )
elements are array boolean[] ( [Z )
implements interface Cloneable ( java.lang.Cloneable )
implements interface Serializable ( java.io.Serializable )
elements are primitive boolean ( boolean )
extends class Object ( java.lang.Object )
extends class Object ( java.lang.Object )
enum RoundingMode ( java.math.RoundingMode )
extends class Enum ( java.lang.Enum )
implements interface Comparable ( java.lang.Comparable )
implements interface Serializable ( java.io.Serializable )
extends class Object ( java.lang.Object )
class ArrayList ( java.util.ArrayList )
implements interface List ( java.util.List )
implements interface Collection ( java.util.Collection )
implements interface Iterable ( java.lang.Iterable )
implements interface RandomAccess ( java.util.RandomAccess )
implements interface Cloneable ( java.lang.Cloneable )
implements interface Serializable ( java.io.Serializable )
extends class AbstractList ( java.util.AbstractList )
implements interface List ( java.util.List )
implements interface Collection ( java.util.Collection )
implements interface Iterable ( java.lang.Iterable )
extends class AbstractCollection ( java.util.AbstractCollection )
implements interface Collection ( java.util.Collection )
implements interface Iterable ( java.lang.Iterable )
extends class Object ( java.lang.Object )
primitive void ( void )
API links
Class.getName()
- Explains the "funky" names for arrays and primitives
References
- Java Tutorials/Reflection API
- See also: Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection
- JLS 15.8.2 Class literals