• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java.lang.Class.getResource() Method  RSS feed

 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I was wondering why you need to retrieve the Class object and then run It's getResource() method. Why does the class type affect the getResource() method.
Doesn't It do the same thing regardless of the Class object the Instance belongs to ?

Basically , why Isn't the getResource() method placed In class Object and accessed directly ?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to be certain, but the idea behind the design of the Object class was only to include methods which would be of general applicability for all classes. At least I think that was the theory.

Class#getResource(String) looks in the location the .class file is found, so it is related to the .class file. Maybe that is why the method is in the Class class.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is because the getResource() method uses a ClassLoader to find the resource to load. Class.getResource() delegates the call to the class loader that loaded the class that you called the method on.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Info !
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!