Reputation: 1313
I'm learining Java reflection API. I have read a lot, use java.lang.reflect
package.
But there is still something I don't understand : the idea that with introspection, you can modify objects at runtime.
Basically you do not need introspection/reflection to do that. With a simple setter() method, you can also modify your object at runtime. What's the difference between using simple setters or using reflection ? In both cases you can achieve the same result.
Upvotes: 0
Views: 244
Reputation: 308001
With reflection you can instantiate objects and call methods that you didn't know about at compile time.
Imagine you had a text file that contained the name of a class. You can use reflection to load and instantiate that class, even if that file didn't exist when your program was compiled.
Similarly, you can use it to build things like generic configuration systems where you create objects and set properties based on some configuration files instead of using hard-coded calls (Spring is one way this can go).
Upvotes: 2