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. Reflection is a built-in mechanism to introspect a Java program at the time of execution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |