Java Class Methods

The Class class in Java is part of the reflection mechanism, providing methods to examine or modify the behavior of methods, classes, and interfaces at runtime. This class allows you to get metadata about the class itself, such as its methods, fields, and constructors, and even instantiate objects at runtime.

This guide covers various methods available in the Class class. Each method is described in simple terms to help beginners understand how to use them. These methods enable dynamic inspection and manipulation of class properties, making it a powerful tool in advanced Java programming.

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

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
getAnnotation() Returns the annotation for the specified type if it is present.
getCanonicalName() Returns the canonical name of the underlying class as defined by the Java Language Specification.
getConstructor() Returns a specific public constructor of the class.
getConstructors() Returns an array of public constructors of the class.
getDeclaredField() Returns a specific declared field of the class.
getDeclaredFields() Returns an array of all declared fields of the class.
getDeclaredMethods() Returns an array of all declared methods of the class.
getEnumConstants() Returns an array of all enum constants of the class.
getField() Returns a specific public field of the class.
getFields() Returns an array of all public fields of the class.
getInterfaces() Returns an array of all interfaces implemented by the class.
getMethods() Returns an array of all public methods of the class.
getModule() Returns the module of the class.
getName() Returns the name of the class as a string.
getPackage() Returns the package of the class.
getPackageName() Returns the package name of the class as a string.
getSimpleName() Returns the simple name of the class as a string.
getSuperclass() Returns the superclass of the class.
getTypeName() Returns the type name of the class as a string.
isAnnotation() Returns true if the class represents an annotation type.
isArray() Returns true if the class represents an array type.
isEnum() Returns true if the class represents an enum type.
isInstance() Returns true if the specified object is an instance of the class.
isInterface() Returns true if ```html the class represents an interface type.
isRecord() Returns true if the class represents a record type.
isSealed() Returns true if the class is a sealed class.
newInstance() Creates a new instance of the class.
arrayType() Returns a Class object representing the array class whose components are of the specified class.
asSubclass() Casts this Class object to represent a subclass of the specified class.
cast() Casts the specified 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.

Comments