Java Programming/API/java.lang.Class

< Java Programming

The Class<T> is defined in the java.lang package.

There is a Class<T> object for each class in your program. The Class object is loaded from <class-name>.class file once when the object of that type is referenced from the first time. After that it stays in memory. The Class object is responsible to creat all the object instances that belong to that class.

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 types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded

Since JDK 1.5 the class java.lang.Class<T> is generic. It's an interesting example of using genericity for something other than a container/collection class.

For example, the type of String.class is Class<String>, and the type of Serializable.class is Class<Serializable>. This can be used to improve the type safety of your reflection code.