|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.lang.Class<T>
, AnnotatedElement
, GenericDeclaration
, Type

public final class Class<T>

, GenericDeclaration
, Type
, 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());
ClassLoader.defineClass(byte[], int, int)
,
Serialized Form| Method Summary | ||
|---|---|---|
|
asSubclass
Casts this Class object to represent a subclass of the class represented by the specified class object. |
|
T |
cast
Casts an object to the class or interface represented by this Class object. |
|
boolean |
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 |
forName
Returns the Class object associated with the class or
interface with the given string name. |
|
static Class |
forName
Returns the Class object associated with the class or
interface with the given string name, using the given class loader. |
|
|
getAnnotation
Returns this element's annotation for the specified type if such an annotation is present, else null. |
|
Annotation |
getAnnotations
Returns all annotations present on this element. |
|
String |
getCanonicalName
Returns the canonical name of the the underlying class as defined by the Java Language Specification. |
|
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 |
getClassLoader
Returns the class loader for the class. |
|
Class |
getComponentType
Returns the Class representing the component type of an
array. |
|
Constructor |
getConstructor
Returns a Constructor object that reflects the specified
public constructor of the class represented by this Class
object. |
|
Constructor |
getConstructors
Returns an array containing Constructor objects reflecting
all the public constructors of the class represented by this
Class object. |
|
Annotation |
getDeclaredAnnotations
Returns all annotations that are directly present on this element. |
|
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 |
getDeclaredConstructor
Returns a Constructor object that reflects the specified
constructor of the class or interface represented by this
Class object. |
|
Constructor |
getDeclaredConstructors
Returns an array of Constructor objects reflecting all the
constructors declared by the class represented by this
Class object. |
|
Field |
getDeclaredField
Returns a Field object that reflects the specified declared
field of the class or interface represented by this Class
object. |
|
Field |
getDeclaredFields
Returns an array of Field objects reflecting all the fields
declared by the class or interface represented by this
Class object. |
|
Method |
getDeclaredMethod
Returns a Method object that reflects the specified
declared method of the class or interface represented by this
Class object. |
|
Method |
getDeclaredMethods
Returns an array of Method objects reflecting all the
methods declared by the class or interface represented by this
Class object. |
|
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 |
getEnclosingClass
Returns the immediately enclosing class of the underlying class. |
|
Constructor |
getEnclosingConstructor
If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing
the immediately enclosing constructor of the underlying
class. |
|
Method |
getEnclosingMethod
If this Class object represents a local or anonymous class within a method, returns a Method object representing the
immediately enclosing method of the underlying class. |
|
T[] |
getEnumConstants
Returns the elements of this enum class or null if this Class object does not represent an enum type. |
|
Field |
getField
Returns a Field object that reflects the specified public
member field of the class or interface represented by this
Class object. |
|
Field |
getFields
Returns an array containing Field objects reflecting all
the accessible public fields of the class or interface represented by
this Class object. |
|
Type |
getGenericInterfaces
Returns the Types representing the interfaces directly implemented by the class or interface represented by this object. |
|
Type |
getGenericSuperclass
Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class. |
|
Class |
getInterfaces
Determines the interfaces implemented by the class or interface represented by this object. |
|
Method |
getMethod
Returns a Method object that reflects the specified public
member method of the class or interface represented by this
Class object. |
|
Method |
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
Returns the Java language modifiers for this class or interface, encoded in an integer. |
|
String |
getName
Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. |
|
Package |
getPackage
Gets the package for this class. |
|
ProtectionDomain |
getProtectionDomain
Returns the ProtectionDomain of this class. |
|
URL |
getResource
Finds a resource with a given name. |
|
InputStream |
getResourceAsStream
Finds a resource with a given name. |
|
Object |
getSigners
Gets the signers of this class. |
|
String |
getSimpleName
Returns the simple name of the underlying class as given in the source code. |
|
Class |
getSuperclass
Returns the Class representing the superclass of the entity
(class, interface, primitive type or void) represented by this
Class. |
|
TypeVariable |
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
Returns true if this Class object represents an annotation type. |
|
boolean |
isAnnotationPresent
Returns true if an annotation for the specified type is present on this element, else false. |
|
boolean |
isAnonymousClass
Returns true if and only if the underlying class is an anonymous class. |
|
boolean |
isArray
Determines if this Class object represents an array class. |
|
boolean |
isAssignableFrom
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
Returns true if and only if this class was declared as an enum in the source code. |
|
boolean |
isInstance
Determines if the specified Object is assignment-compatible
with the object represented by this Class. |
|
boolean |
isInterface
Determines if the specified Class object represents an
interface type. |
|
boolean |
isLocalClass
Returns true if and only if the underlying class is a local class. |
|
boolean |
isMemberClass
Returns true if and only if the underlying class is a member class. |
|
boolean |
isPrimitive
Determines if the specified Class object represents a
primitive type. |
|
boolean |
isSynthetic
Returns true if this class is a synthetic class; returns false otherwise. |
|
T |
newInstance
Creates a new instance of the class represented by this Class object. |
|
String |
toString
Converts the object to a string. |
|
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Method Detail |
|---|

public String![]()
![]()
toString()
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".
toString

in class Object


public static Class![]()
![]()
<?> forName(String
![]()
![]()
className) throws ClassNotFoundException
![]()
![]()
Class object associated with the class or
interface with the given string name. Invoking this method is
equivalent to:
whereClass.forName(className, true, currentLoader)
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.
className - the fully qualified name of the desired class.
Class object for the class with the
specified name.
LinkageError

- if the linkage fails
ExceptionInInitializerError

- if the initialization provoked
by this method fails
ClassNotFoundException

- if the class cannot be located

public static Class![]()
![]()
<?> forName(String
![]()
![]()
name, boolean initialize, ClassLoader
![]()
![]()
loader) throws ClassNotFoundException
![]()
![]()
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.
name - fully qualified name of the desired classinitialize - whether the class must be initializedloader - class loader from which the class must be loaded
LinkageError

- if the linkage fails
ExceptionInInitializerError

- if the initialization provoked
by this method fails
ClassNotFoundException

- if the class cannot be located by
the specified class loaderforName(String)
,
ClassLoader


public