Class
object. For example, the type of String.class
is Class<String>
. Use Class<?>
if the class being modeled is
unknown.
Class
represent classes and
interfaces in a running Java application. An enum class and a record
class are kinds of class; an annotation interface 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 a Class
object is constructed automatically by the Java Virtual Machine when
a class is derived from the bytes of a class
file through
the invocation of one of the following methods:
ClassLoader::defineClass
java.lang.invoke.MethodHandles.Lookup::defineClass
java.lang.invoke.MethodHandles.Lookup::defineHiddenClass
The methods of class Class
expose many characteristics of a
class or interface. Most characteristics are derived from the class
file that the class loader passed to the Java Virtual Machine or
from the class
file passed to Lookup::defineClass
or Lookup::defineHiddenClass
.
A few characteristics are determined by the class loading environment
at run time, such as the module returned by getModule()
.
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()); }
Class
object for a named
class or interface (or for void
) using a class literal.
For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());
Some methods of class Class
expose whether the declaration of
a class or interface in Java source code was enclosed within
another declaration. Other methods describe how a class or interface
is situated in a nest. A nest is a set of
classes and interfaces, in the same run-time package, that
allow mutual access to their private
members.
The classes and interfaces are known as nestmates.
One nestmate acts as the
nest host, and enumerates the other nestmates which
belong to the nest; each of them in turn records it as the nest host.
The classes and interfaces which belong to a nest, including its host, are
determined when
class
files are generated, for example, a Java compiler
will typically record a top-level class as the host of a nest where the
other members are the classes and interfaces whose declarations are
enclosed within the top-level class declaration.
Lookup::defineHiddenClass
is a hidden
class or interface.
All kinds of class, including enum classes and record classes, may be
hidden classes; all kinds of interface, including annotation interfaces,
may be hidden interfaces.
The name of a hidden class or interface is
not a binary name,
which means the following:
Class::describeConstable
,
ClassDesc::of
, or
ClassDesc::ofDescriptor
.
Class::forName
or ClassLoader::loadClass
.
Class
.
HelloWorld.java
, creates a
similarly-named class
file, HelloWorld.class
, where
the class stored in that class
file is named "HelloWorld"
, matching the base names of the source and class
files.
For the Class
object of an implicitly declared class HelloWorld
, the methods to get the name and
type name return results
equal to "HelloWorld"
. The simple name of such an implicitly declared class is "HelloWorld"
and
the canonical name is "HelloWorld"
.
java.lang.ClassLoader#defineClass(byte[], int, int)
Modifier and Type | Class and Description |
---|---|
private static class | |
private static class | Class.
Atomic operations support. |
private static class | |
private static class |
Modifier and Type | Field and Description |
---|---|
private static ProtectionDomain | allPermDomain
protection domain returned when the internal domain is null |
private static final int | |
private transient volatile Class. | |
private transient volatile AnnotationType | |
private transient volatile Constructor | |
private transient Object | |
private final ClassLoader | |
private transient volatile int | |
pack-priv transient ClassValue. | |
private final Class | |
private static final Class | |
private static final int | |
private transient volatile Map | |
private transient volatile T[] | |
private transient volatile ClassRepository | |
private transient Module | |
private transient String | |
private transient String | |
private transient volatile SoftReference | |
private static ReflectionFactory | |
private static final ObjectStreamField[] | serialPersistentFields
Class Class is special cased within the Serialization Stream Protocol. |
private static final long | serialVersionUID
use serialVersionUID from JDK 1.1 for interoperability |
private static final int |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public Set | Returns: an unmodifiable set of the access flags for this class, possibly emptyReturns an unmodifiable set of the access flags for this class, possibly empty. |
private static void | |
private void | |
private Class. | |
private static boolean | |
public Class | Returns: aClass describing the array typeImplements java. Returns a |
public < the type to cast this U> ClassClass object to | Returns: thisClass object, cast to represent a subclass of
the specified class object.the class of the type to cast this clazz)Class object toCasts this |
private String | |
pack-priv boolean | |
public T | |
private void | checkMemberAccess(SecurityManager sm, int which, Class<?> caller, boolean checkProxyInterfaces)
References Deprecated
SecurityManager is deprecated or references (maybe indirectly) at least one deprecated element.
|
private void | checkPackageAccess(SecurityManager sm, final ClassLoader ccl, boolean checkProxyInterfaces)
References Deprecated
SecurityManager is deprecated or references (maybe indirectly) at least one deprecated element.
|
private static void | checkPackageAccessForPermittedSubclasses(SecurityManager sm, final ClassLoader ccl, Class<?>[] subClasses)
References Deprecated
SecurityManager is deprecated or references (maybe indirectly) at least one deprecated element.
|
public Class | Returns: aClass describing the component type, or null
if this Class does not describe an array typeImplements java. Returns the component type of this |
private static <U> Constructor | |
private static Field[] | |
private static Method[] | |
private Class. | |
public Optional | describeConstable()
Implements java. Returns a nominal descriptor for this instance, if one can be
constructed, or an empty |
public String | Returns: the descriptor string for thisClass objectImplements java. Returns the descriptor string of the entity (class, interface, array class,
primitive type, or |
public boolean | Returns: the desired assertion status of the specified class.Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked. |
private static native boolean | |
private Class | |
pack-priv Map | |
pack-priv Method | Returns: theMethod object for the method found from this class matching
the specified name and parameters, or null if not foundtrue if only public methods are examined, otherwise all methods publicOnly, String the name of the method name, Class<?>... the parameter array parameterTypes)Returns the most specific |
public static Class | Returns: theClass object for the class with the
specified name.the binary name
of the class or the string representing an array type className)Returns the |
private static Class | |
public static Class | Returns: class object representing the desired classthe binary name
of the class or the string representing an array class name, boolean if initialize, ClassLoader true the class will be initialized
(which implies linking). See Section 12.4 of The Java Language
Specification.class loader from which the class must be loaded loader)Returns the |
private static Class | |
public static Class | Returns: Class object of the given name defined in the given module;
null if not found.A module module, String The binary name
of the class name)Returns the |
private static Class | |
private static native Class | forName0(String name, boolean initialize, ClassLoader loader, Class<?> caller)
Called after security check for system loader access checks have been made. |
public static Class | forPrimitiveName(String
the name of the primitive type to find primitiveName)Returns the |
public AnnotatedType[] | Returns: an array representing the superinterfacesReturns an array of |
public AnnotatedType | Returns: an object representing the superclassReturns an |
public <A extends Annotation> A | getAnnotation(Class<A>
the Class object corresponding to the
annotation type annotationClass)Implements java. Returns this element's annotation for the specified type if such an annotation is present, else null. |
public Annotation[] | getAnnotations()
Implements java. Returns annotations that are present on this element. |
public <A extends Annotation> A[] | getAnnotationsByType(Class<A>
the Class object corresponding to the
annotation type annotationClass)Overrides default java. Returns annotations that are associated with this element. |
pack-priv AnnotationType | |
public String | Returns: the canonical name of the underlying class if it exists, andnull otherwise.Returns the canonical name of the underlying class as defined by The Java Language Specification. |
private String | |
private int | |
private native int | |
pack-priv Object | |
public Class | Returns: the array ofClass objects representing the public
members of this classReturns an array containing |
private int | |
private native int | |
public ClassLoader | Returns: the class loader that loaded the class or interface represented by thisClass object.Returns the class loader for the class. |
pack-priv ClassLoader | |
public Class | Returns: theClass representing the component type of this
class if this class is an arrayReturns the |
pack-priv native ConstantPool | |
public Constructor | Returns: theConstructor object of the public constructor that
matches the specified parameterTypes the parameter array parameterTypes)Returns a |
private Constructor | |
public Constructor | Returns: the array ofConstructor objects representing the
public constructors of this classReturns an array containing |
public <A extends Annotation> A | getDeclaredAnnotation(Class<A>
the Class object corresponding to the
annotation type annotationClass)Overrides default java. Returns this element's annotation for the specified type if such an annotation is directly present, else null. |
pack-priv Map | |
public Annotation[] | getDeclaredAnnotations()
Implements java. Returns annotations that are directly present on this element. |
public <A extends Annotation> A[] | getDeclaredAnnotationsByType(Class<A>
the Class object corresponding to the
annotation type annotationClass)Overrides default java. Returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present. |
public Class | Returns: the array ofClass objects representing all the
declared members of this classReturns an array of |
private native Class | |
public Constructor | Returns: TheConstructor object for the constructor with the
specified parameter listthe parameter array parameterTypes)Returns a |
public Constructor | Returns: the array ofConstructor objects representing all the
declared constructors of this classReturns an array of |
private native Constructor | |
public Field | Returns: theField object for the specified field in this
classthe name of the field name)Returns a |
public Field[] | Returns: the array ofField objects representing all the
declared fields of this classReturns an array of |
private native Field[] | |
public Method | Returns: theMethod object for the method of this class
matching the specified name and parametersthe name of the method name, Class<?>... the parameter array parameterTypes)Returns a |
public Method[] | Returns: the array ofMethod objects representing all the
declared methods of this classReturns an array containing |
private native Method[] | |
pack-priv List | Returns: the list ofMethod objects for the public methods of
this class matching the specified name and parametersthe name of the method name, Class<?>... the parameter array parameterTypes)Returns the list of |
public Class | Returns: the declaring class for this classIf the class or interface represented by this |
private native Class | |
public Class | Returns: the immediately enclosing class of the underlying classReturns the immediately enclosing class of the underlying class. |
public Constructor | Returns: the immediately enclosing constructor of the underlying class, if that class is a local or anonymous class; otherwisenull .If this |
public Method | Returns: the immediately enclosing method of the underlying class, if that class is a local or anonymous class; otherwisenull .If this |
private native Object[] | |
private Class. | |
public T[] | Returns: an array containing the values comprising the enum class represented by thisClass object in the order they're
declared, or null if this Class object does not
represent an enum classReturns the elements of this enum class or null if this Class object does not represent an enum class. |
pack-priv T[] | getEnumConstantsShared()
Returns the elements of this enum class or null if this Class object does not represent an enum class; identical to getEnumConstants except that the result is uncloned, cached, and shared by all callers. |
pack-priv static byte[] | |
private GenericsFactory | |
public Field | |
private Field | |
public Field[] | Returns: the array ofField objects representing the
public fieldsReturns an array containing |
private ClassRepository | |
public Type[] | Returns: an array of interfaces directly implemented by this classReturns the |
private native String | |
public Type | Returns: the direct superclass of the class represented by thisClass objectReturns the |
public Class | Returns: an array of interfaces directly implemented by this classReturns the interfaces directly implemented by the class or interface
represented by this |
private Class | |
private native Class | |
public Method | Returns: theMethod object that matches the specified
name and parameterTypes the name of the method name, Class<?>... the list of parameters parameterTypes)Returns a |
private Method | |
public Method[] | Returns: the array ofMethod objects representing the
public methods of this classReturns an array containing |
private PublicMethods. | getMethodsRecursive(String name, Class<?>[] parameterTypes, boolean includeStatic, boolean publicOnly)
|
public native int | Returns: theint representing the modifiers for this classReturns the Java language modifiers for this class or interface, encoded in an integer. |
public Module | Returns: the module that this class or interface is a member ofReturns the module that this class or interface is a member of. |
public String | Returns: the name of the class, interface, or other entity represented by thisClass object.Returns the name of the entity (class, interface, array class,
primitive type, or void) represented by this |
public Class | Returns: the nest host of this class or interfaceReturns the nest host of the nest to which the class
or interface represented by this |
private native Class | |
public Class | Returns: an array of all classes and interfaces in the same nest as this class or interfaceReturns an array containing |
private native Class | |
public Package | |
public String | |
public Class | Returns: an array ofClass objects of the permitted subclasses of this class or interface,
or null if this class or interface is not sealed.Returns an array containing |
private native Class | |
pack-priv static native Class | |
public ProtectionDomain | Returns: the ProtectionDomain of this classReturns the |
private native ProtectionDomain | |
pack-priv native byte[] | |
pack-priv native byte[] | |
public RecordComponent[] | Returns: An array ofRecordComponent objects representing all the
record components of this record class, or null if this
class is not a record classReturns an array of |
private native RecordComponent[] | |
private static ReflectionFactory | |
public URL | Returns: Ajava. object; null if no resource with
this name is found, the resource cannot be located by a URL, the
resource is in a package that is not
open to at least the caller
module, or access to the resource is denied by the security
manager.name of the desired resource name)Finds a resource with a given name. |
public InputStream | Returns: Ajava. object; null if no
resource with this name is found, the resource is in a package
that is not open to at
least the caller module, or access to the resource is denied
by the security manager.name of the desired resource name)Finds a resource with a given name. |
public native Object[] | Returns: the signers of this class, or null if there are no signers. In particular, this method returns null if thisClass object represents
a primitive type or void.Gets the signers of this class. |
private String | getSimpleBinaryName()
Returns the "simple binary name" of the underlying class, i.e., the binary name without the leading enclosing class name. |
private native String | |
public String | Returns: the simple name of the underlying classReturns the simple name of the underlying class as given in the source code. |
private String | |
public native Class | Returns: the direct superclass of the class represented by thisClass objectReturns the |
public String | Returns: an informative string for the name of this class or interfaceOverrides default java. Return an informative string for the name of this class or interface. |
public TypeVariable | Returns: an array ofTypeVariable objects that represent
the type variables declared by this generic declarationImplements java. Returns an array of |
private boolean | |
private boolean | |
private native String | |
public boolean | Returns: true if this Class object represents an annotation
interface; false otherwiseReturns true if this |
public boolean | isAnnotationPresent(Class<? extends Annotation>
the Class object corresponding to the
annotation type annotationClass)Overrides default java. Returns true if an annotation for the specified type is present on this element, else false. |
public boolean | Returns: true if and only if this class is an anonymous class.Returns |
public native boolean | Returns: true if this Class object represents an array class;
false otherwise.Implements java. Determines if this |
public native boolean | Returns: theboolean value indicating whether objects of the
type cls can be assigned to objects of this classthe cls)Class object to be checkedDetermines if the class or interface represented by this
|
private boolean | |
public boolean | Returns: true if and only if this class was declared as an enum in the source codeReturns true if and only if this class was declared as an enum in the source code. |
public native boolean | Returns: true if and only if this class is a hidden class.Returns |
public native boolean | Returns: true ifobj is an instance of this classthe object to check obj)Determines if the specified |
public native boolean | Returns: true if this Class object represents an interface;
false otherwise.Determines if this |
public boolean | Returns: true if and only if this class is a local class.Returns |
private boolean | |
public boolean | Returns: true if and only if this class is a member class.Returns |
public boolean | Returns: true if this class and c are members of
the same nest; and false otherwise.the class to check c)Determines if the given |
private boolean | isOpenToCaller(String name, Class<?> caller)
Returns true if a resource with the given name can be located by the given caller. |
public native boolean | Returns: true if and only if this class represents a primitive typeImplements java. Determines if this |
public boolean | Returns: true if and only if this class is a record class, otherwise falseReturns |
private native boolean | |
public boolean | Returns: true if and only if this Class object represents
a sealed class or interface.Returns |
public boolean | Returns: true if and only if this class has the synthetic modifier
bit setReturns |
private boolean | |
private String | methodToString(String name, Class<?>[] argTypes)
Helper method to get the method name from arguments. |
public T | Returns: a newly allocated instance of the class represented by this object.
Deprecated
since 9. This method propagates any exception thrown by the
nullary constructor, including a checked exception.
Creates a new instance of the class represented by this |
private Class. | newReflectionData(SoftReference<Class.
|
private Constructor | |
private Field[] | |
private Method[] | |
private Field[] | |
private Method[] | |
pack-priv ProtectionDomain | |
private Class. | |
private static native void | |
private String | |
private static Field | |
private static Method | |
pack-priv native void | |
private static Class | |
public String | Returns: a string describing thisClass , including
information about modifiers and type parametersReturns a string describing this |
public String | Returns: a string representation of thisClass object.Overrides java. Converts the object to a string. |
pack-priv static String |