Disadvantages of java reflection

java reflection mechanism can be called to the private method, is not to destroy the JAVA trigram encapsulation.

This is a very worthwhile exploration of the issue, many people contact reflection, reflection of the strong function of the majority of people will be skeptical, the feeling that seriously undermines the nature of encapsulation. But what is encapsulation and what is security?

Encapsulation, is to hide the specific details of the implementation, and the function as a whole to provide the external use of the class, that is to say, the public method can complete the function of the class has. When someone else uses the class, if they call the private methods directly through reflection, they may not be able to realize the functionality of the class at all, and they may even make mistakes, so calling private methods through reflection can be said to be useless, and there is no need for the developer to intentionally destroy the encapsulated class. From this point of view, encapsulation is not broken.

Security, if it means protecting the implementation source code from being seen by others, serves no purpose. It’s easy to get the source code without reflection.

So I thought that the reflection mechanism just provides a powerful feature that enables developers to implement something outside of encapsulation, as per specific needs. It’s like nuclear technology, although it’s dangerous to build a nuclear bomb, it’s still useful to build a nuclear power plant (this analogy doesn’t seem to be very appropriate, so let’s leave it at that).

We have discussed this issue with our friends, and I feel that we have not explained it very well, so let’s discuss it together and see how it can be expressed in a way that is easier to understand:)

The three methods of reflection in java are?

First: through the forName() method;

Second: class.class;

Third: object.getClass().

An example is as follows:






Class<? >c1=null;

Class<? >c2=


Class<? >c3=


//Three ways to instantiate by reflection


//The most common form



}catch( ClassNotFoundException




//Instantiated via method in Object class



//Instantiated via class.class



System.out.println(“Class name: “+c1.getName());

//Get class name

System.out.println(” Class name: “+c2.getName());

//get class name

System.out.println(“Class name: “+c3.getName());


How to understand reflection in Java, what is the role

Reflection is the ability to examine metadata and gather type information about it. Metadata (the most basic unit of data after compilation) is a large set of tables. When compiling an assembly or module, the compiler creates a class definition table, a field definition table, a method definition table, and so on.

The main purpose of reflection is to extend the system and call the assembly dynamically.

Extending the system means writing the system first, defining the interfaces within the system, and then the developer writing the code for the interfaces later.

At this point the system should use reflection, the system uses reflection to call the interface, when the interface is not written, the system will not use the reflection will not be wrong, at this point it is the same as not realizing this function, when the interface is written, the system will automatically call the interface function is displayed on the system.

That is, reflection to achieve plug-and-play functionality.

Dynamic invocation of an assembly is the use of reflection to call a compiled dll, but of course the dll is not referenced in the project you are building.

When you compile your project, you will not compile the dll you call, when you run to call the dll, only then will go to call the dll, to determine whether there is a syntax semantics, such as compilation, runtime errors.

This use of reflection has a certain degree of flexibility, that is, you do not have to call the dll from your project, there is also the dll can be changed at any time (of course, the interface still need to correspond), do not need to change the project you built.

All in all, the best thing about reflection is that you don’t have to write a good dll after you create a new project, but you can write a dll later after you create a new project. This is called late binding. Of course, the use of reflection is resource-consuming, loss of efficiency, if not in the above occasions to use this technology, may not bring benefits, but rather bad.

Why is java reflection more time consuming than serial under concurrency?

Some Java Virtual Machine optimizations cannot be performed because reflection involves dynamically resolved types. As a result, reflection operations perform slower than their non-reflection counterparts and should be avoided in code sections that are frequently called in performance-sensitive applications.

If multi-threaded access is involved, that will inevitably result in automatic locking of the class, so switching between threads occurs, with additional time consumption

In java, reflection, the following statements about the reflection mechanism are false ()

B. Reflection mechanism refers to the process of loading and using a class through the “.class” file during the compilation of a program.

If there are two programmers and one programmer is writing a program that needs to use a class written by the second programmer, but the second programmer has not finished the class he wrote. This is not going to pass compilation. Using the mechanism of Java reflection, it is possible for the first programmer to complete the compilation of his own code without getting the class written by the second programmer.

There are two types of compilation in Java:

Static compilation: one-time compilation. Compiles all your modules in at compile time.

Dynamic compilation: on-demand compilation. Compile whichever module is used when the program is running.

If you do not understand, then give a business scenario to help you understand: for example, the development of a reader to support txt, pdf, doc three formats. We read txt, read pdf, read doc defined as three functional modules.

Java’s new framework for the extensive use of reflection on the implementation of the efficiency of a great impact

Now almost all of the java framework (such as spring, struts, mybatis, etc.) are the use of reflection for the core basic operations. Reflection has long been the basic set of java programming. So don’t worry about this.