Similarly, we can retrieve field information of the Integer class. The getDeclaredFields() method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object, and so on.įor example, we easily can list all the public methods of the Object class as follows: Method methods = Object.
Similarly, the getModifiers() method returns Java language modifiers the getConstructors() method returns an array which contains Constructor objects reflecting all the public constructors of the class represented by this Class object.
For example, the getName() method of the Class can be used to obtain the full qualified name of the class it represents. The Class class provides a set of methods to get the class name, its methods, modifiers, constructors, and so on. Alternatively, the static forName() method defined in the Class also can be used to load an unknown class and get a reference of its Class object. We can obtain a reference of the instantiated Class object by using this method. As we know, the Object class is the parent of all the classes in Java it offers a method called getClass().
Serializable, GenericDeclaration, Type, AnnotatedElementĪny class loaded into Java Runtime creates an instance of Class which represents the loaded class or an interface. public final class Class extends Object implements It is a final class hence, it cannot be extended. The class named Class is the most important class in the Reflection API and the basis of introspection and reflection mechanism. Structurally, the APIs are not different from the Java language and comprise all the language elements such as classes, methods, fields, constructors, interfaces, annotations, and so forth. This is done dynamically the compiler has no knowledge of the classes that are loaded at runtime.
Using this API an executing code can ask an object for its class, explore its methods, the parameters it takes, return types, and constructors. It enables us to explore not only the intrinsic nature of the class but also instantiate one without using an explicit new operator at runtime. The Reflection API is a part of the Standard Java API Library. This article attempts to explore some of the intricacies and glimpses upon its effective use. This powerful feature can be used to expand the capabilities of a program, to examine the class or object internals at runtime. It can be used to inspect, modify, and transform a Java program without affecting existing code.
Class Example Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name.In this Java Reflection Tutorial series, we will discuss many of the different aspects of Java Reflection API. If you have been around for a short while and are familiar with Java and the tools used in the industry then you have already been using Reflection by some of these tools and applications. Determine a class’s structure at runtime (name, fields, methods)Īlthough there are many Java Reflection Tutorials available online, I hope that this Java Reflection Tutorial will give you a deeper understanding of how you can use many of the features available.We can construct objects dynamically at runtime.