


Probably the most commonly used method of Class is getName. Just like an Employee object describes the properties of a particular employee, a Class object describes the properties of a particular class. The getClass() method in the Object class returns an instance of Class type. The class that holds this information is called, somewhat confusingly, Class. However, you can also access this information by working with a special Java class. Runtime type information is used by the virtual machine to select the correct methods to execute. This information keeps track of the class to which each object belongs. While your program is running, the Java runtime system always maintains what is called runtime type identification on all objects. If you are interested in programming applications rather than tools for other Java programmers, you can safely skip the remainder of this chapter and return to it later. Reflection is a powerful and complex mechanism however, it is of interest mainly to tool builders, not application programmers. Take advantage of Method objects that work just like function pointers in languages such as C++.
#JAVA REFLECTION CALL STATIC METHOD CODE#
Implement generic array manipulation code and.Inspect objects at runtime, for example, to write a single toString method that works for all classes.Analyze the capabilities of classes at runtime.As the next sections show, you can use it to The reflection mechanism is extremely powerful. In particular, when new classes are added at design or runtime, rapid application development tools can dynamically inquire about the capabilities of the classes that were added.Ī program that can analyze the capabilities of classes is called reflective. Using reflection, Java can support tools like the ones to which users of Visual Basic have grown accustomed. This feature is heavily used in JavaBeans, the component architecture for Java (see Volume II for more on JavaBeans). The reflection library gives you a very rich and elaborate toolset to write programs that manipulate Java code dynamically. Core Java, Volume I-Fundamentals, 8th Edition
