![]() Therefore, the operations performed by reflections are usually slow. Since the types in reflection are resolved dynamically, JVM (Java Virtual Machine) optimization cannot take place. For example, if one access the private members of a class and sets null value to it, then the other user of the same class can get the NullReferenceException, and this behaviour is not expected.Īnother demerit is the overhead in performance. Thus, reflection may leak important data to the outside world, which is dangerous. It is possible to access the private methods and fields of a class using reflection. It helps in the creation of Visual Development Environments and class browsers which provides aid to the developers to write the correct code.Ĭons: Using reflection, one can break the principles of encapsulation. It is also possible to call methods, instantiate a clear or to set the value of fields using reflection. Pros: Inspection of interfaces, classes, methods, and fields during runtime is possible using reflection, even without using their names during the compile time. While the reflection provides a lot of advantages, it has some disadvantages too. Java reflection should always be used with caution. Let's see the simple example of forName() method.
0 Comments
Leave a Reply. |