Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Dec 8, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Risks of Using Reflection

Reflection plays an essential role in adding dynamism to Java. Using reflection, an application can dynamically (i.e., at run time) determine the state and behavior of any class. Reflection allows the developer to create objects that can construct new class instances and new arrays, access and modify fields of objects and classes, invoke methods on objects and classes, and access and modify the elements of arrays.

However, using the features provided by the reflection mechanism in Java requires careful consideration. The reflection API should be used only as needed and after taking into account the costs associated with its usage. Reflection method calls have substantial performance overhead. Reflection-related code lends to overall complexity in the application. Return values from reflection method calls almost always have to be cast to the right type, making the code type-unsafe and less efficient. Errors in method invocation occur at run time instead of being detected during compilation. As a result, applications are more likely to contain bugs.

Reflection should be used only when no other simpler object-oriented language mechanisms can be used to accomplish the same task.

Ajit Sagar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date