Home >Java >javaTutorial >What is the use of java reflection mechanism?

What is the use of java reflection mechanism?

藏色散人
藏色散人Original
2019-04-28 13:51:279061browse


#This article mainly introduces the use of java reflection mechanism. I hope it will be helpful to friends in need!

What is the use of java reflection mechanism?

#Java language reflection provides a versatile method of dynamically linking program components. It allows a program to create and control objects of any class (subject to security restrictions) without having to hardcode the target class in advance. These properties make reflection particularly suitable for creating libraries that work with objects in very common ways. For example, reflection is often used in frameworks that persist objects in databases, XML, or other external formats. Java reflection is very useful because it allows classes and data structures to dynamically retrieve relevant information by name and allows this information to be manipulated in a running program. This feature of Java is very powerful and is not available in other commonly used languages, such as C, C, Fortran or Pascal.

But reflection has two disadvantages. The first is a performance issue. Reflection is much slower than direct code when used for field and method access. The extent of the performance problem depends on how reflection is used in the program. If it were a relatively infrequent part of the program's operation, slow performance wouldn't be a problem. Even the worst-case timing graph in the test shows that the reflection operation takes only a few microseconds. Performance issues become critical only when reflection is used in the core logic of performance-critical applications.

One of the more serious drawbacks in many applications is that using reflection obscures what is actually happening inside the program. Programmers hope to see the logic of the program in the source code. Technologies such as reflection that bypass the source code will cause maintenance problems. Reflected code is more complex than the corresponding direct code, as can be seen in the code examples for performance comparisons. The best solution to these problems is to use reflection conservatively - only where it can really increase flexibility - and document its use in the target class.

In short, reflection in most cases is to improve the flexibility of the program. Therefore, it is often used in general frameworks. Because the framework needs to be applicable to more situations. High requirements for flexibility.

Related recommendations: "Java Tutorial"


The above is the detailed content of What is the use of java reflection mechanism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn