GeezerVison is showing docs.oracle.com — Class (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
compact1, compact2, compact3
java.lang
Class Class<T>
-
- Type Parameters:
T - the type of the class modeled by this Class
object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is
unknown.
- All Implemented Interfaces:
- Serializable, AnnotatedElement, GenericDeclaration, Type
public final class Class<T>
extends Object
implements Serializable, 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. See Section 15.8.2 of
The Java™ Language Specification.
For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());
- Since:
- JDK1.0
- See Also:
ClassLoader.defineClass(byte[], int, int),
Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
<U> Class<? extends U> |
asSubclass(Class<U> clazz) |
Casts this Class object to represent a subclass of the class
represented by the specified class object.
|
T |
cast(Object obj) |
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(String className) |
Returns the Class object associated with the class or
interface with the given string name.
|
static Class<?> |
forName(String name,
boolean initialize,
ClassLoader loader) |
Returns the Class object associated with the class or
interface with the given string name, using the given class loader.
|
AnnotatedType[] |
getAnnotatedInterfaces() |
Returns an array of AnnotatedType objects that represent the use
of types to specify superinterfaces of the entity represented by this
Class object.
|
AnnotatedType |
getAnnotatedSuperclass() |
Returns an AnnotatedType object that represents the use of a
type to specify the superclass of the entity represented by this Class object.
|
<A extends Annotation> A |
getAnnotation(Class<A> annotationClass) |
Returns this element's annotation for the specified type if
such an annotation is present, else null.
|
Annotation[] |
getAnnotations() |
Returns annotations that are present on this element.
|
<A extends Annotation> A[] |
getAnnotationsByType(Class<A> annotationClass) |
Returns annotations that are associated with this element.
|
String |
getCanonicalName() |
Returns the canonical name of 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<T> |
getConstructor(Class<?>... parameterTypes) |
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.
|
<A extends Annotation> A |
getDeclaredAnnotation(Class<A> annotationClass) |
Returns this element's annotation for the specified type if
such an annotation is directly present, else null.
|
Annotation[] |
getDeclaredAnnotations() |
Returns annotations that are directly present on this element.
|
<A extends Annotation> A[] |
getDeclaredAnnotationsByType(Class<A> annotationClass) |
Returns this element's annotation(s) for the specified type if
such annotations are either directly present or
indirectly present.
|
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<T> |
getDeclaredConstructor(Class<?>... parameterTypes) |
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(String name) |
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(String name,
Class<?>... parameterTypes) |
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 containing Method objects reflecting all the
declared methods of the class or interface represented by this Class object, including public, protected, default (package)
access, and private methods, but excluding inherited methods.
|
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(String name) |
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(String name,
Class<?>... parameterTypes) |
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 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(String name) |
Finds a resource with a given name.
|
InputStream |
getResourceAsStream(String name) |
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<? super T> |
getSuperclass() |
Returns the Class representing the superclass of the entity
(class, interface, primitive type or void) represented by this
Class.
|
String |
getTypeName() |
Return an informative string for the name of this type.
|
TypeVariable<Class<T>>[] |
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(Class<? extends Annotation> annotationClass) |
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(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() |
Returns true if and only if this class was declared as an enum in the
source code.
|
boolean |
isInstance(Object obj) |
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 |
toGenericString() |
Returns a string describing this Class, including
information about modifiers and type parameters.
|
String |
toString() |
Converts the object to a string.
|
-
-
Method Detail
-
toString
public 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 in class Object
- Returns:
- a string representation of this class object.
-
getName
public String getName()
Returns the name of the entity (class, interface, array class,
primitive type, or void) represented by this Class object,
as a String.
If this class object represents a reference type that is not an
array type then the binary name of the class is returned, as specified
by
The Java™ Language Specification.
If this class object represents a primitive type or void, then the
name returned is a String equal to the Java language
keyword corresponding to the primitive type or void.
If this class object represents a class of arrays, then the internal
form of the name consists of the name of the element type preceded by
one or more '[' characters representing the depth of the array
nesting. The encoding of element type names is as follows:
| Element Type | | Encoding
|
|---|
| boolean | | Z
| | byte | | B
| | char | | C
| | class or interface
| | Lclassname;
| | double | | D
| | float | | F
| | int | | I
| | long | | J
| | short | | S
|
The class or interface name classname is the binary name of
the class specified above.
Examples:
String.class.getName()
returns "java.lang.String"
byte.class.getName()
returns "byte"
(new Object[3]).getClass().getName()
returns "[Ljava.lang.Object;"
(new int[3][4][5][6][7][8][9]).getClass().getName()
returns "[[[[[[[I"
- Returns:
- the name of the class or interface
represented by this object.
-
getTypeParameters
public TypeVariable<Class<T>>[] 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. Returns an
array of length 0 if the underlying generic declaration declares no type
variables.
- Specified by:
getTypeParameters in interface GenericDeclaration
- Returns:
- an array of
TypeVariable objects that represent
the type variables declared by this generic declaration
- Throws:
GenericSignatureFormatError - if the generic
signature of this generic declaration does not conform to
the format specified in
The Java™ Virtual Machine Specification
- Since:
- 1.5
-
getSuperclass
public Class<? super T> getSuperclass()
Returns the Class representing the superclass of the entity
(class, interface, primitive type or void) represented by this
Class. If this Class represents either the
Object class, an interface, a primitive type, or void, then
null is returned. If this object represents an array class then the
Class object representing the Object class is
returned.
- Returns:
- the superclass of the class represented by this object.
-
getDeclaringClass
public Class<?> getDeclaringClass()
throws SecurityException
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. This method returns
null if this class or interface is not a member of any other class. If
this Class object represents an array class, a primitive
type, or void,then this method returns null.
- Returns:
- the declaring class for this class
- Throws:
SecurityException - If a security manager, s, is present and the caller's
class loader is not the same as or an ancestor of the class
loader for the declaring class and invocation of s.checkPackageAccess()
denies access to the package of the declaring class
- Since:
- JDK1.1
-
getCanonicalName
public String getCanonicalName()
Returns the canonical name of the underlying class as
defined by the Java Language Specification. Returns null if
the underlying class does not have a canonical name (i.e., if
it is a local or anonymous class or an array whose component
type does not have a canonical name).
- Returns:
- the canonical name of the underlying class if it exists, and
null otherwise.
- Since:
- 1.5
-
getClasses
public 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. This includes public
class and interface members inherited from superclasses and public class
and interface members declared by the class. This method returns an
array of length 0 if this Class object has no public member
classes or interfaces. This method also returns an array of length 0 if
this Class object represents a primitive type, an array
class, or void.
- Returns:
- the array of
Class objects representing the public
members of this class
- Throws:
SecurityException - If a security manager, s, is present and
the caller's class loader is not the same as or an
ancestor of the class loader for the current class and
invocation of s.checkPackageAccess() denies access to the package
of this class.
- Since:
- JDK1.1
-
getConstructors
public Constructor<?>[] getConstructors()
throws SecurityException
Returns an array containing Constructor objects reflecting
all the public constructors of the class represented by this
Class object. An array of length 0 is returned if the
class has no public constructors, or if the class is an array class, or
if the class reflects a primitive type or void.
Note that while this method returns an array of Constructor<T> objects (that is an array of constructors from
this class), the return type of this method is Constructor<?>[] and not Constructor<T>[] as
might be expected. This less informative return type is
necessary since after being returned from this method, the
array could be modified to hold Constructor objects for
different classes, which would violate the type guarantees of
Constructor<T>[].
- Returns:
- the array of
Constructor objects representing the
public constructors of this class
- Throws:
SecurityException - If a security manager, s, is present and
the caller's class loader is not the same as or an
ancestor of the class loader for the current class and
invocation of s.checkPackageAccess() denies access to the package
of this class.
- Since:
- JDK1.1
-
getDeclaredClasses
public Class<?>[] getDeclaredClasses()
throws SecurityException
Returns an array of Class objects reflecting all the
classes and interfaces declared as members of the class represented by
this Class object. This includes public, protected, default
(package) access, and private classes and interfaces declared by the
class, but excludes inherited classes and interfaces. This method
returns an array of length 0 if the class declares no classes or
interfaces as members, or if this Class object represents a
primitive type, an array class, or void.
- Returns:
- the array of
Class objects representing all the
declared members of this class
- Throws:
SecurityException - If a security manager, s, is present and any of the
following conditions is met:
- the caller's class loader is not the same as the
class loader of this class and invocation of
s.checkPermission method with
RuntimePermission("accessDeclaredMembers")
denies access to the declared classes within this class
- the caller's class loader is not the same as or an
ancestor of the class loader for the current class and
invocation of
s.checkPackageAccess() denies access to the package
of this class
- Since:
- JDK1.1
-
getDeclaredConstructor
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
Returns a Constructor object that reflects the specified
constructor of the class or interface represented by this
Class object. The parameterTypes parameter is
an array of Class objects that identify the constructor's
formal parameter types, in declared order.
If this Class object represents an inner class
declared in a non-static context, the formal parameter types
include the explicit enclosing instance as the first parameter.
- Parameters:
parameterTypes - the parameter array
- Returns:
- The
Constructor object for the constructor with the
specified parameter list
- Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - If a security manager, s, is present and any of the
following conditions is met:
- the caller's class loader is not the same as the
class loader of this class and invocation of
s.checkPermission method with
RuntimePermission("accessDeclaredMembers")
denies access to the declared constructor
- the caller's class loader is not the same as or an
ancestor of the class loader for the current class and
invocation of
s.checkPackageAccess() denies access to the package
of this class
- Since:
- JDK1.1
-
getProtectionDomain
public ProtectionDomain getProtectionDomain()
Returns the ProtectionDomain of this class. If there is a
security manager installed, this method first calls the security
manager's checkPermission method with a
RuntimePermission("getProtectionDomain") permission to
ensure it's ok to get the
ProtectionDomain.
- Returns:
- the ProtectionDomain of this class
- Throws:
SecurityException - if a security manager exists and its
checkPermission method doesn't allow
getting the ProtectionDomain.
- Since:
- 1.2
- See Also:
ProtectionDomain,
SecurityManager.checkPermission(java.security.Permission),
RuntimePermission
-
getAnnotation
public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
Returns this element's annotation for the specified type if
such an annotation is present, else null.
- Specified by:
getAnnotation in interface AnnotatedElement
- Type Parameters:
A - the type of the annotation to query for and return if present
- Parameters:
annotationClass - the Class object corresponding to the
annotation type
- Returns:
- this element's annotation for the specified annotation type if
present on this element, else null
- Throws:
NullPointerException - if the given annotation class is null
- Since:
- 1.5
-
getAnnotations
public Annotation[] getAnnotations()
Returns annotations that are present on this element.
If there are no annotations present on this element, the return
value is an array of length 0.
The caller of this method is free to modify the returned array; it will
have no effect on the arrays returned to other callers.
- Specified by:
getAnnotations in interface AnnotatedElement
- Returns:
- annotations present on this element
- Since:
- 1.5
-
getDeclaredAnnotation
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass)
Returns this element's annotation for the specified type if
such an annotation is directly present, else null.
This method ignores inherited annotations. (Returns null if no
annotations are directly present on this element.)
- Specified by:
getDeclaredAnnotation in interface AnnotatedElement
- Type Parameters:
A - the type of the annotation to query for and return if directly present
- Parameters:
annotationClass - the Class object corresponding to the
annotation type
- Returns:
- this element's annotation for the specified annotation type if
directly present on this element, else null
- Throws:
NullPointerException - if the given annotation class is null
- Since:
- 1.8
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Returns annotations that are directly present on this element.
This method ignores inherited annotations.
If there are no annotations directly present on this element,
the return value is an array of length 0.
The caller of this method is free to modify the returned array; it will
have no effect on the arrays returned to other callers.
- Specified by:
getDeclaredAnnotations in interface AnnotatedElement
- Returns:
- annotations directly present on this element
- Since:
- 1.5
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|