Java Class Methods

The Class class in Java, part of the java.lang package, represents classes and interfaces in a running Java application. It provides methods for examining the runtime properties of the class and creating new class instances, among other things. These methods are essential for reflection in Java.

Java Class Methods

The table below contains various methods of the Java Class class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
arrayType() Returns a Class object representing an array class containing the component type.
asSubclass() Cast this Class object to represent a subclass of the specified class.
cast() Casts an object to the class represented by this Class object.
getComponentType() Returns the Class representing the component type of an array.
forName() Returns the Class object associated with the class or interface with the given string name.
getAnnotatedInterfaces() Returns an array of AnnotatedType objects that represent the use of interfaces by this class or interface.
getAnnotation() Returns this element’s annotation for the specified type if such an annotation is present.
getCanonicalName() Returns the canonical name of the underlying class as defined by the Java Language Specification.
getConstructor() Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
getConstructors() Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
getDeclaredField() Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
getDeclaredFields() Returns an array of Field objects reflecting all the declared fields of the class or interface represented by this Class object.
getDeclaredMethods() Returns an array of Method objects reflecting all the declared methods of the class or interface represented by this Class object.
getEnumConstants() Returns the elements of this enum class or null if this Class object does not represent an enum type.
getField() Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
getFields() Returns an array of Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
getInterfaces() Returns an array of Class objects representing all the interfaces implemented by the class or interface represented by this Class object.
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.
getModule() Returns the module of the class or interface represented by this Class object.
getName() Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
getPackage() Returns the package for this class.
getPackageName() Returns the package name of the class represented by this Class object.
getSimpleName() Returns the simple name of the underlying class as given in the source code.
getSuperclass() Returns the superclass of the class represented by this Class object.
getTypeName() Returns the name of the type represented by this Class object as a String.
isAnnotation() Determines if the specified Class object represents an annotation type.
isArray() Determines if this Class object represents an array class.
isEnum() Determines if the specified Class object represents an enum type.
isInstance() Determines if the specified object is an instance of the class or interface represented by this Class object.
isInterface() Determines if the specified Class object represents an interface type.
isRecord() Determines if the specified Class object represents a record class.
isSealed() Determines if the specified Class object represents a sealed class or interface.
newInstance() Creates a new instance of the class represented by this Class object.

For more detailed information, please refer to the official Java SE Documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top