TheClass
<T> is defined in thejava.lang
package.
There is aClass
<T> object for each class in your program. TheClass
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. TheClass
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 aClass
object that is shared by all arrays with the same element type and number of dimensions. Theprimitive types (boolean
,byte
,char
,short
,int
,long
,float
, anddouble
), and the keywordvoid
are also represented asClass
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 classjava.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.