I have a line of code like this:
ConfiguationManagerUtils.class.getResource(resourceName);
I don't understand why reflection is used here. What is the difference between calling it like a static class method:
ConfiguationManagerUtils.getResource(resourceName);
It's not using reflection at all. The getResource(String)
method called in your first snippet simply isn't declared on ConfigurationManagerUtils
- it's declared on the Class
class, as an instance method. If the second code snippet works as well, that's because there's a static getResource(String)
method declared in ConfigurationManagerUtils
(or a superclass). That may well do something entirely different to Class.getResource()
.
The first snippet is just using a class literal (ConfigurationManagerUtils.class
) to obtain a reference to a Class
instance on which it can call the getResource(String)
instance method.
See more on this question at Stackoverflow