Java reflection mechanism-what is, why, and how to use it ., Java --
When we face a new knowledge, we often need to know what it is, what it can do, where it is better than the original knowledge, and how we should use it. When you are able to solve these problems, it means that you are already getting started with this knowledge.
I. What is it?
Java Reflaction in Action can be explained in this sentence. Reflection is the ability of a running program to check itself and the software runtime environment. It can be changed based on what it discovers. In general, reflection can obtain class information based on the specified class name at runtime.
Ii. Why
Why should we use reflection? What is its role? What is its application in actual programming.
First, let's clarify two concepts: static compilation and dynamic compilation.
Static Compilation: determine the type during compilation, bind the object, that is, pass.
Dynamic Compilation: determines the type during runtime and binds the object. Dynamic compilation maximizes java flexibility and reflects many
To reduce the coupling between classes.
We can clearly see the benefits of dynamic compilation, and reflection uses dynamic compilation to create objects.
So let's take a look at the advantages of reflection in practice?
The comparison is often more intuitive to show us the differences between the two.
First explain from a code case (apply an example of a blog: http://blog.csdn.net/justdoit_potato/article/details/51011843)
If reflection is not needed, it is like this.
Interface fruit {public abstract void eat ();} class Apple implements fruit {public void eat () {System. out. println ("Apple") ;}} class Orange implements fruit {public void eat () {System. out. println ("Orange ");}} // construct the Factory class // That is to say, if we add other instances in the future, we only need to modify the Factory class. class Factory {public static fruit getInstance (String fruitName) {fruit f = null; if ("Apple ". equals (fruitName) {f = new Apple ();} if ("Orange ". equals (fruitName) {f = new Orange ();} return f ;}} class hello {public static void main (String [] a) {fruit f = Factory. getInstance ("Orange"); f. eat ();}}
It can be found that whenever we want to add a new kind of fruit, we will have to change the source code in the Factory, and often changing the original correct code is a very dangerous behavior. And with the increase in the variety of fruits, you will find that your factory class will become more and more bloated,
I have to say that this is a very -- Practice. (Beginners may ask why we don't directly add new fruit to the main method. We may need the getInstance method to do something else... So it is not new directly );
Reflection is undoubtedly a smart way to look at the code.
interface fruit{ public abstract void eat(); } class Apple implements fruit{ public void eat(){ System.out.println("Apple"); } } class Orange implements fruit{ public void eat(){ System.out.println("Orange"); } } class Factory{ public static fruit getInstance(String ClassName){ fruit f=null; try{ f=(fruit)Class.forName(ClassName).newInstance(); }catch (Exception e) { e.printStackTrace(); } return f; } } class hello{ public static void main(String[] a){ fruit f=Factory.getInstance("Reflect.Apple"); if(f!=null){ f.eat(); } } }
When new varieties of fruit appear, you do not need to modify the original code.
From the above case, we can clearly understand the superiority of reflection.
Some people may ask, this example can be fully understood, but if we put it into practical programming and application, under what circumstances will we use reflection?
For example, in actual development, we need to create a new class in a package, but the class in this package always needs to change. What should we do, do you always modify xxx = new xxx () in the main method. This is undoubtedly troublesome. Reflection is used. We can add a configuration file to record all the class names in the package, add a class name when adding classes in the package, and delete a Class Name When deleting the package. Let the main method read the class name in this configuration file and get the instance through reflection. We do not need to modify the code in the main method.
What is the use of reflection? You can even modify the private attributes of other classes. In android development, when we need to change a private flag, the android Source Code does not provide the set method, and we cannot change the source code. What should we do? reflection can perfectly solve this problem.
So many times, why not all reflection is used in our development? One reason is the overhead. What is its overhead expensive? Try to use reflection wherever necessary.
Iii. How to Use
After talking about what it is and why, we must understand how to use reflection. First, let's look at the methods involved in reflection.
Class c = Class. forName ("className"); Note: className must be the full name, that is, including the package name, for example, cn. netjava. pojo. UserInfo;
Object obj = c. newInstance (); // create an Object instance
OK. If you have an object, everything is easy to do. If you want any information, there will be any information.
Methods for obtaining Constructors
Constructor getConstructor (Class [] params) // obtain the public Constructor Based on the specified parameter
Constructor [] getConstructors () // obtain all constructors of public
Constructor getDeclaredConstructor (Class [] params) // obtain the public and non-public constructors Based on the specified parameters.
Constructor [] getDeclaredConstructors () // obtain all public Constructors
Methods for obtaining class methods
Method getMethod (String name, Class [] params). Obtain the Method based on the Method name and parameter type.
Method [] getMethods () // obtain all public methods
Method getDeclaredMethod (String name, Class [] params) // obtain public and non-public methods based on the Method name and parameter type.
Method [] getDeclaredMethods () // obtain the public and non-public methods
Method for obtaining attributes in a class
Field getField (String name) // obtain the corresponding public Variable Based on the variable name
Field [] getFields () // obtain the public method in the class
Field getDeclaredField (String name) // obtain public and non-public variables based on the method name
Field [] getDeclaredFields () // obtain all public and non-public methods in the class
When you see these methods, you can understand how powerful reflection is. When you use these methods correctly, you basically master the reflection skills.
Reference blog: http://www.cnblogs.com/jqyp/archive/2012/03/29/2423112.html