I was going through ImageIO, and I read this code on web, but could not figure out why getClass() is needed in this scenario. (getClass() returns the classname as far as I have understood -> correct me if I am wrong). so, why we need getClass() here?
Asked
Active
Viewed 42 times
0
-
Well, you are wrong. It dosn't get the class name, it gets the class **object**. You need a class object or a class loader object to get a resource. – RealSkeptic Sep 04 '17 at 14:01
1 Answers
1
The .getResource()
method of a Class<?>
object (which is what any .getClass()
call will return) ultimately calls upon the ClassLoader
of the running JVM; and you need a ClassLoader
in order to access resources in your running class path.
This is documented in the relevant javadoc:
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.

fge
- 119,121
- 33
- 254
- 329