Reputation: 687
Why public InputStream getResourceAsStream(String name) is in Class class? It just give inputstream of file which is in jar file and there is no relation with Class class. so it can be static method and it can be in any class.
Upvotes: 8
Views: 937
Reputation: 718826
It just give inputstream of file which is in jar file ...
Incorrect. Not all classloaders load resources from regular JAR file.
All of this complexity is hidden from you when you use the ClassLoader
API via Class
in this case.
... and there is no relation with Class class.
Incorrect. See @Jon Skeet's answer. Note that calling Class.getResourceAsStream(String)
gives a resource that belongs to the same security context as the class. This can be very important if there are multiple classloaders / security contexts in use.
Upvotes: 2
Reputation: 1500575
There is a relationship to the class:
getResourceAsStream("baz.txt")
on the class for foo.bar.SomeClass
it will look for /foo/bar/baz.txt
Upvotes: 12