![java reflection method java reflection method](https://www.concretepage.com/java/jdk-8/images/java-8-reflection-access-to-parameter-names-of-method-and-constructor-with-maven-gradle-and-eclipse-using-parameters-compiler-argument.jpg)
Let us first take a look at the dynamic loading and instantiation of classes using a simple example. It is also the key to the functionality of the Reflection API. The class Class provides methods for querying properties of the class and allows classes to be loaded dynamically and instances to be generated dynamically. For each class that the runtime system uses, a class object of the Class type is created during the loading process. With the getClass method of the Object class, any object can deliver a suitable class object. The Object class is the parent class of all other classes and ensures that some elementary methods such as equals, toString, clone or hashCode are available in all classes.
![java reflection method java reflection method](https://3.bp.blogspot.com/-AwB9vOQV-gI/UDkm0TnyEoI/AAAAAAAAAxM/_b96uJnMxCU/s1600/method+in+reflection+API+2.jpg)
So a mechanism was sought that made these runtime system capabilities, which are normally requested by the compiler, available to "normal" applications. Furthermore, static or instance-based methods should be called and member variables should be accessible even if their name is only known when the program is running.
![java reflection method java reflection method](https://3.bp.blogspot.com/-LyQlEYlZMsc/W17MdEDTy5I/AAAAAAAAC7U/cmMp7RB7wdwjqqfkANSk-0G-NPhdxQXRgCLcBGAs/s1600/Field-Reflection-class.png)
Rather, what was needed was the ability to load and instantiate classes (also with parameterized constructors) without their name having to be known at compile time. In particular, the development of the bean and serialization APIs was not possible with the language properties available in version 1.0. While this is not a problem for most common applications, it is inadequate for developing generic tools and highly configurable applications that are extensible with plug-ins.
#Java reflection method code#
In order to create an object, to call one of its methods or to access one of its member variables, the code of the class had to be known at compile time.
![java reflection method java reflection method](https://www.nowonbun.com/contents/69/279_001.jpg)
Testing.When developing JDK 1.1, the developers were confronted with a weakness in Java that made it impossible to develop certain types of tools and libraries: the static structure of classes and objects. Public static void main(String args) throws Exception ) We cast the returned value of the invocation to a String and we display the result to the console.įollowing this, we make a similar call to setString1() and another call to getString1(). For the first parameter of invoke() we pass in the Testing object on which we'd like to call the method, and the second parameter of invoke() is an array of Objects that represent the parameter values that we'd like to pass to the method when we invoke it. These are in package, so you have to import them. We invoke the getString1() method on our Testing object by calling invoke() on the Method object. This re- quires using classes like Field, Method, and Modifier. This feature is really powerful, but as always, has to be used with some good judgement. Since getString1() has no parameters, this array is empty. Then you have probably already heared of Java’s Reflection API, and in case you haven’t, this is a good opportunity to see what it’s all about, and what it can be used for. The first parameter is the getMethod() call is the method name, and the second call is an array of Class objects representing the parameters of the getString1() method. Next, a Method object is obtained for the getString1() method of Testing by calling the getMethod() method of the Testing Class object that we have. A 'Testing' object is instantiated, and its class object is obtained via a call to getClass() on the object. This is demonstrated in the ClassMethodTest class below. In Java S W, it is possible to invoke a method on an object or class using the reflection API.