java.lang
Class Class<T>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.lang.Class<T>
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , AnnotatedElement sample code for java.lang.reflect.AnnotatedElement definition code for java.lang.reflect.AnnotatedElement , GenericDeclaration sample code for java.lang.reflect.GenericDeclaration definition code for java.lang.reflect.GenericDeclaration , Type sample code for java.lang.reflect.Type definition code for java.lang.reflect.Type

public final class Class<T>
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable , GenericDeclaration sample code for java.lang.reflect.GenericDeclaration definition code for java.lang.reflect.GenericDeclaration , Type sample code for java.lang.reflect.Type definition code for java.lang.reflect.Type , AnnotatedElement sample code for java.lang.reflect.AnnotatedElement definition code for java.lang.reflect.AnnotatedElement

Instances of the class 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.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

The following example uses a Class object to print the class name of an object:

     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }
 

It is also possible to get the Class object for a named type (or for void) using a class literal (JLS Section 15.8.2). For example:

     System.out.println("The name of class Foo is: "+Foo.class.getName());
 

Since:
JDK1.0
See Also:
ClassLoader.defineClass(byte[], int, int) sample code for java.lang.ClassLoader.defineClass(byte[], int, int) definition code for java.lang.ClassLoader.defineClass(byte[], int, int) , Serialized Form

Method Summary
<U> Class sample code for java.lang.Class definition code for java.lang.Class <? extends U>
asSubclass sample code for java.lang.Class.asSubclass(java.lang.Class) definition code for java.lang.Class.asSubclass(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <U> clazz)
          Casts this Class object to represent a subclass of the class represented by the specified class object.
 T cast sample code for java.lang.Class.cast(java.lang.Object) definition code for java.lang.Class.cast(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Casts an object to the class or interface represented by this Class object.
 boolean desiredAssertionStatus sample code for java.lang.Class.desiredAssertionStatus() definition code for java.lang.Class.desiredAssertionStatus() ()
          Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
static Class sample code for java.lang.Class definition code for java.lang.Class <?> forName sample code for java.lang.Class.forName(java.lang.String) definition code for java.lang.Class.forName(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  className)
          Returns the Class object associated with the class or interface with the given string name.
static Class sample code for java.lang.Class definition code for java.lang.Class <?> forName sample code for java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) definition code for java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (String sample code for java.lang.String definition code for java.lang.String  name, boolean initialize, ClassLoader sample code for java.lang.ClassLoader definition code for java.lang.ClassLoader  loader)
          Returns the Class object associated with the class or interface with the given string name, using the given class loader.
<A extends Annotation sample code for java.lang.annotation.Annotation definition code for java.lang.annotation.Annotation >
A
getAnnotation sample code for java.lang.Class.getAnnotation(java.lang.Class) definition code for java.lang.Class.getAnnotation(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <A> annotationClass)
          Returns this element's annotation for the specified type if such an annotation is present, else null.
 Annotation sample code for java.lang.annotation.Annotation definition code for java.lang.annotation.Annotation [] getAnnotations sample code for java.lang.Class.getAnnotations() definition code for java.lang.Class.getAnnotations() ()
          Returns all annotations present on this element.
 String sample code for java.lang.String definition code for java.lang.String getCanonicalName sample code for java.lang.Class.getCanonicalName() definition code for java.lang.Class.getCanonicalName() ()
          Returns the canonical name of the the underlying class as defined by the Java Language Specification.
 Class sample code for java.lang.Class definition code for java.lang.Class [] getClasses sample code for java.lang.Class.getClasses() definition code for java.lang.Class.getClasses() ()
          Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.
 ClassLoader sample code for java.lang.ClassLoader definition code for java.lang.ClassLoader getClassLoader sample code for java.lang.Class.getClassLoader() definition code for java.lang.Class.getClassLoader() ()
          Returns the class loader for the class.
 Class sample code for java.lang.Class definition code for java.lang.Class <?> getComponentType sample code for java.lang.Class.getComponentType() definition code for java.lang.Class.getComponentType() ()
          Returns the Class representing the component type of an array.
 Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor <T> getConstructor sample code for java.lang.Class.getConstructor(java.lang.Class...) definition code for java.lang.Class.getConstructor(java.lang.Class...) (Class sample code for java.lang.Class definition code for java.lang.Class ... parameterTypes)
          Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
 Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor [] getConstructors sample code for java.lang.Class.getConstructors() definition code for java.lang.Class.getConstructors() ()
          Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
 Annotation sample code for java.lang.annotation.Annotation definition code for java.lang.annotation.Annotation [] getDeclaredAnnotations sample code for java.lang.Class.getDeclaredAnnotations() definition code for java.lang.Class.getDeclaredAnnotations() ()
          Returns all annotations that are directly present on this element.
 Class sample code for java.lang.Class definition code for java.lang.Class [] getDeclaredClasses sample code for java.lang.Class.getDeclaredClasses() definition code for java.lang.Class.getDeclaredClasses() ()
          Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
 Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor <T> getDeclaredConstructor sample code for java.lang.Class.getDeclaredConstructor(java.lang.Class...) definition code for java.lang.Class.getDeclaredConstructor(java.lang.Class...) (Class sample code for java.lang.Class definition code for java.lang.Class ... parameterTypes)
          Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
 Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor [] getDeclaredConstructors sample code for java.lang.Class.getDeclaredConstructors() definition code for java.lang.Class.getDeclaredConstructors() ()
          Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
 Field sample code for java.lang.reflect.Field definition code for java.lang.reflect.Field getDeclaredField sample code for java.lang.Class.getDeclaredField(java.lang.String) definition code for java.lang.Class.getDeclaredField(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
 Field sample code for java.lang.reflect.Field definition code for java.lang.reflect.Field [] getDeclaredFields sample code for java.lang.Class.getDeclaredFields() definition code for java.lang.Class.getDeclaredFields() ()
          Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
 Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method getDeclaredMethod sample code for java.lang.Class.getDeclaredMethod(java.lang.String, java.lang.Class...) definition code for java.lang.Class.getDeclaredMethod(java.lang.String, java.lang.Class...) (String sample code for java.lang.String definition code for java.lang.String  name, Class sample code for java.lang.Class definition code for java.lang.Class ... parameterTypes)
          Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
 Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method [] getDeclaredMethods sample code for java.lang.Class.getDeclaredMethods() definition code for java.lang.Class.getDeclaredMethods() ()
          Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.
 Class sample code for java.lang.Class definition code for java.lang.Class <?> getDeclaringClass sample code for java.lang.Class.getDeclaringClass() definition code for java.lang.Class.getDeclaringClass() ()
          If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
 Class sample code for java.lang.Class definition code for java.lang.Class <?> getEnclosingClass sample code for java.lang.Class.getEnclosingClass() definition code for java.lang.Class.getEnclosingClass() ()
          Returns the immediately enclosing class of the underlying class.
 Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor <?> getEnclosingConstructor sample code for java.lang.Class.getEnclosingConstructor() definition code for java.lang.Class.getEnclosingConstructor() ()
          If this Class object represents a local or anonymous class within a constructor, returns a Constructor sample code for java.lang.reflect.Constructor definition code for java.lang.reflect.Constructor object representing the immediately enclosing constructor of the underlying class.
 Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method getEnclosingMethod sample code for java.lang.Class.getEnclosingMethod() definition code for java.lang.Class.getEnclosingMethod() ()
          If this Class object represents a local or anonymous class within a method, returns a Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method object representing the immediately enclosing method of the underlying class.
 T[] getEnumConstants sample code for java.lang.Class.getEnumConstants() definition code for java.lang.Class.getEnumConstants() ()
          Returns the elements of this enum class or null if this Class object does not represent an enum type.
 Field sample code for java.lang.reflect.Field definition code for java.lang.reflect.Field getField sample code for java.lang.Class.getField(java.lang.String) definition code for java.lang.Class.getField(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
 Field sample code for java.lang.reflect.Field definition code for java.lang.reflect.Field [] getFields sample code for java.lang.Class.getFields() definition code for java.lang.Class.getFields() ()
          Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
 Type sample code for java.lang.reflect.Type definition code for java.lang.reflect.Type [] getGenericInterfaces sample code for java.lang.Class.getGenericInterfaces() definition code for java.lang.Class.getGenericInterfaces() ()
          Returns the Types representing the interfaces directly implemented by the class or interface represented by this object.
 Type sample code for java.lang.reflect.Type definition code for java.lang.reflect.Type getGenericSuperclass sample code for java.lang.Class.getGenericSuperclass() definition code for java.lang.Class.getGenericSuperclass() ()
          Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.
 Class sample code for java.lang.Class definition code for java.lang.Class [] getInterfaces sample code for java.lang.Class.getInterfaces() definition code for java.lang.Class.getInterfaces() ()
          Determines the interfaces implemented by the class or interface represented by this object.
 Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method getMethod sample code for java.lang.Class.getMethod(java.lang.String, java.lang.Class...) definition code for java.lang.Class.getMethod(java.lang.String, java.lang.Class...) (String sample code for java.lang.String definition code for java.lang.String  name, Class sample code for java.lang.Class definition code for java.lang.Class ... parameterTypes)
          Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
 Method sample code for java.lang.reflect.Method definition code for java.lang.reflect.Method [] getMethods sample code for java.lang.Class.getMethods() definition code for java.lang.Class.getMethods() ()
          Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
 int getModifiers sample code for java.lang.Class.getModifiers() definition code for java.lang.Class.getModifiers() ()
          Returns the Java language modifiers for this class or interface, encoded in an integer.
 String sample code for java.lang.String definition code for java.lang.String getName sample code for java.lang.Class.getName() definition code for java.lang.Class.getName() ()
          Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
 Package sample code for java.lang.Package definition code for java.lang.Package getPackage sample code for java.lang.Class.getPackage() definition code for java.lang.Class.getPackage() ()
          Gets the package for this class.
 ProtectionDomain sample code for java.security.ProtectionDomain definition code for java.security.ProtectionDomain getProtectionDomain sample code for java.lang.Class.getProtectionDomain() definition code for java.lang.Class.getProtectionDomain() ()
          Returns the ProtectionDomain of this class.
 URL sample code for java.net.URL definition code for java.net.URL getResource sample code for java.lang.Class.getResource(java.lang.String) definition code for java.lang.Class.getResource(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Finds a resource with a given name.
 InputStream sample code for java.io.InputStream definition code for java.io.InputStream getResourceAsStream sample code for java.lang.Class.getResourceAsStream(java.lang.String) definition code for java.lang.Class.getResourceAsStream(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Finds a resource with a given name.
 Object sample code for java.lang.Object definition code for java.lang.Object [] getSigners sample code for java.lang.Class.getSigners() definition code for java.lang.Class.getSigners() ()
          Gets the signers of this class.
 String sample code for java.lang.String definition code for java.lang.String getSimpleName sample code for java.lang.Class.getSimpleName() definition code for java.lang.Class.getSimpleName() ()
          Returns the simple name of the underlying class as given in the source code.
 Class sample code for java.lang.Class definition code for java.lang.Class <? super T> getSuperclass sample code for java.lang.Class.getSuperclass() definition code for java.lang.Class.getSuperclass() ()
          Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
 TypeVariable sample code for java.lang.reflect.TypeVariable definition code for java.lang.reflect.TypeVariable <Class sample code for java.lang.Class definition code for java.lang.Class <T>>[] getTypeParameters sample code for java.lang.Class.getTypeParameters() definition code for java.lang.Class.getTypeParameters() ()
          Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
 boolean isAnnotation sample code for java.lang.Class.isAnnotation() definition code for java.lang.Class.isAnnotation() ()
          Returns true if this Class object represents an annotation type.
 boolean isAnnotationPresent sample code for java.lang.Class.isAnnotationPresent(java.lang.Class) definition code for java.lang.Class.isAnnotationPresent(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <? extends Annotation sample code for java.lang.annotation.Annotation definition code for java.lang.annotation.Annotation > annotationClass)
          Returns true if an annotation for the specified type is present on this element, else false.
 boolean isAnonymousClass sample code for java.lang.Class.isAnonymousClass() definition code for java.lang.Class.isAnonymousClass() ()
          Returns true if and only if the underlying class is an anonymous class.
 boolean isArray sample code for java.lang.Class.isArray() definition code for java.lang.Class.isArray() ()
          Determines if this Class object represents an array class.
 boolean isAssignableFrom sample code for java.lang.Class.isAssignableFrom(java.lang.Class) definition code for java.lang.Class.isAssignableFrom(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <?> cls)
          Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
 boolean isEnum sample code for java.lang.Class.isEnum() definition code for java.lang.Class.isEnum() ()
          Returns true if and only if this class was declared as an enum in the source code.
 boolean isInstance sample code for java.lang.Class.isInstance(java.lang.Object) definition code for java.lang.Class.isInstance(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Determines if the specified Object is assignment-compatible with the object represented by this Class.
 boolean isInterface sample code for java.lang.Class.isInterface() definition code for java.lang.Class.isInterface() ()
          Determines if the specified Class object represents an interface type.
 boolean isLocalClass sample code for java.lang.Class.isLocalClass() definition code for java.lang.Class.isLocalClass() ()
          Returns true if and only if the underlying class is a local class.
 boolean isMemberClass sample code for java.lang.Class.isMemberClass() definition code for java.lang.Class.isMemberClass() ()
          Returns true if and only if the underlying class is a member class.
 boolean isPrimitive sample code for java.lang.Class.isPrimitive() definition code for java.lang.Class.isPrimitive() ()
          Determines if the specified Class object represents a primitive type.
 boolean isSynthetic sample code for java.lang.Class.isSynthetic() definition code for java.lang.Class.isSynthetic() ()
          Returns true if this class is a synthetic class; returns false otherwise.
 T newInstance sample code for java.lang.Class.newInstance() definition code for java.lang.Class.newInstance() ()
          Creates a new instance of the class represented by this Class object.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.lang.Class.toString() definition code for java.lang.Class.toString() ()
          Converts the object to a string.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Method Detail

toString sample code for java.lang.Class.toString() definition code for java.lang.Class.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName. If this Class object represents a primitive type, this method returns the name of the primitive type. If this Class object represents void this method returns "void".

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a string representation of this class object.

forName sample code for java.lang.Class.forName(java.lang.String) definition code for java.lang.Class.forName(java.lang.String)

public static Class sample code for java.lang.Class definition code for java.lang.Class <?> forName(String sample code for java.lang.String definition code for java.lang.String  className)
                        throws ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException 
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
  Class.forName(className, true, currentLoader)
 
where currentLoader denotes the defining class loader of the current class.

For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:

   Class t = Class.forName("java.lang.Thread")
 

A call to forName("X") causes the class named X to be initialized.

Parameters:
className - the fully qualified name of the desired class.
Returns:
the Class object for the class with the specified name.
Throws:
LinkageError sample code for java.lang.LinkageError definition code for java.lang.LinkageError - if the linkage fails
ExceptionInInitializerError sample code for java.lang.ExceptionInInitializerError definition code for java.lang.ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException - if the class cannot be located

forName sample code for java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) definition code for java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader)

public static Class sample code for java.lang.Class definition code for java.lang.Class <?> forName(String sample code for java.lang.String definition code for java.lang.String  name,
                               boolean initialize,
                               ClassLoader sample code for java.lang.ClassLoader definition code for java.lang.ClassLoader  loader)
                        throws ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException 
Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

If name denotes an array class, the component type of the array class is loaded but not initialized.

For example, in an instance method the expression:

  Class.forName("Foo")
 
is equivalent to:
  Class.forName("Foo", true, this.getClass().getClassLoader())
 
Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.

If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.

Parameters:
name - fully qualified name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
Returns:
class object representing the desired class
Throws:
LinkageError sample code for java.lang.LinkageError definition code for java.lang.LinkageError - if the linkage fails
ExceptionInInitializerError sample code for java.lang.ExceptionInInitializerError definition code for java.lang.ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException - if the class cannot be located by the specified class loader
Since:
1.2
See Also:
forName(String) sample code for java.lang.Class.forName(java.lang.String) definition code for java.lang.Class.forName(java.lang.String) , ClassLoader sample code for java.lang.ClassLoader definition code for java.lang.ClassLoader

newInstance sample code for java.lang.Class.newInstance() definition code for java.lang.Class.newInstance()

public