Reputation: 126
Lots of websites and books I have read have stated that:
relying on
reflection
in Java is a bad idea and you should seek other ways of testing/interfacing with encapsulated objects.
However, I can't find any actual reasons for this, is it to do with security managers being different from JVM to JVM? Or are there other reasons on top of this?
Upvotes: 0
Views: 87
Reputation: 433
Reflection can be very helpful in quite a few ways. One of the best examples is creating a program that allows for third party plugins. Reflection is great for that. However, using it otherwise results in the potential for a lot of other problems, including more runtime errors instead of compile errors, slower invocation, and (not always a HUGE problem) refactor and code analysis problems with IDE’s.
Generally, if you think you can do something without reflection, then do it without reflection. There is a very small grey area where reflection is a good idea. Finding that grey area takes practice.
Upvotes: 6