Results 1 to 12 of 12
Thread: getClass().getResource()
- 03-22-2012, 04:35 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
- 03-22-2012, 04:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: getClass().getResource()
getClass() is defined in the Object class to return the "runtime class of this Object". So, when that line of code is executed it returns a Class instance representing the class of whatever instance that line appears in. (It will be in a constructor or method and some instance will be involved.)
Following the trail to the Class class, we see that getResource() "finds a resource with a given name". What it returns is a URL of the resource and how that URL is determined from the name you give it ("exit.png") is documented. In the simple case where the line is part of some class represented as a .class file on your disk somewhere then the returned URL will be a File:// url for a sibling file with the name "exit.png". It is worth getting used to some of the complexities of how the URL is constructed as they give you flexibility in how you organise your resources. See, for instance, the JavaWorld article Got Resources?.
Altogether then, getClass().getResource("exit.png")) returns a URL. And there is an ImageIcon constructor that "creates an ImageIcon from the specified URL".Last edited by pbrockway2; 03-24-2012 at 11:09 PM. Reason: typo in last line
- 03-24-2012, 01:48 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: getClass().getResource()
In all the examples of getClass() that I have seen,it returns the runtime class of an object provided the object name is specified or equivalently if the "this" keyword is used. getClass().getResource()
The above line satisfies none of the cases.Which class is returned in such a case.
Really appreciate your help.
Thank You.Last edited by forwardbias; 03-24-2012 at 01:51 PM.
- 03-24-2012, 11:36 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: getClass().getResource()
As the quoted bit of the API docs state, getClass() returns "runtime class of this Object". Unless you are resolving an ambiguity of some kind there is no need to use the "this" keyword: method() and this.method() mean the same thing.
This is quite easily verified by printing the return values involved:
The printf() is just showing off (but it *is* a useful method):Java Code:public class GetClassEg { void test() { System.out.println("getClass(): " + getClass()); System.out.println("this.getClass(): " + this.getClass()); // Even more convincingly System.out.printf("getClass(): %1$h=%1$s%n", getClass()); System.out.printf("this.getClass(): %1$h=%1$s%n", this.getClass()); // Or System.out.println("The same? " + (getClass() == this.getClass())); } public static void main(String[] args) { new GetClassEg().test(); } }
% convert
1$ first argument
h as hex string (a bit like System.identityHashCode())
% convert
1$ first argument
s as ordinary string (like toString())
%n new line
-----
I corrected a typo in the very last line of my previous post.
- 03-25-2012, 01:55 AM #5
Re: getClass().getResource()
Maybe this will help you understand. The expression
is equivalent toJava Code:getClass().getResource("exit.png");
except that in the first case, there isn't a named variable holding the value returned by getClass().Java Code:Class klass = getClass(); klass.getResource("exit.png");Get in the habit of using standard Java naming conventions!
- 03-25-2012, 04:39 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: getClass().getResource()
So in this case it is returning the object that invokes the class?
- 03-25-2012, 06:18 PM #7
Re: getClass().getResource()
getClass() returns a Class. getClass().getResource() calls getResource() on that Class. And Class.getResource() returns a URL. So the whole expression evaluates to a URL.
Get in the habit of using standard Java naming conventions!
- 03-28-2012, 05:11 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: getClass().getResource()
"Instances of the Class represent classes and interfaces in a running Java application"
This is what I know about Class.So the Class and getResource() combine to form the URL of the image.
The Class gives the URL of the current directory and the getResource of the image?
Thank for bearing with me
- 03-28-2012, 05:56 AM #9
Re: getClass().getResource()
Yeah, this is kind of a mind-bender.
There is a class called Class. An object of type Class represents a class or interface in the JVM.
There is also a class called Object. Every class is derived from Object, so every object is an Object and inherits the methods of the Object class.
One of those methods is getClass(), which returns the Class representing the object's class.
Still with me?
There is a method in the Class class called getResource(). It simply finds the named resource and returns the URL for it.Get in the habit of using standard Java naming conventions!
- 03-28-2012, 04:35 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Re: getClass().getResource()
Isn't it the same as saying "the class of the object that invokes the method containing getClass() is returned by getClass()"
Thank You VVVV Much
- 03-28-2012, 06:01 PM #11
Re: getClass().getResource()
Something like that. It would be a little more accurate to say that "getClass() returns the class of the object on which getClass() is invoked." And even more accurate to say that "getClass() returns a Class object representing the class of the object on which getClass() is invoked."
Get in the habit of using standard Java naming conventions!
- 03-29-2012, 03:10 AM #12
Member
- Join Date
- Mar 2012
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Java getClass()
By hqt in forum New To JavaReplies: 2Last Post: 01-31-2012, 08:44 PM -
How to change FileOutputStream to class.getResource()
By gomdohri in forum New To JavaReplies: 1Last Post: 10-27-2011, 10:13 AM -
using getField method of getClass
By jdigger in forum New To JavaReplies: 2Last Post: 02-02-2011, 11:16 PM -
ClassLoader getResource Problem
By sh4dyPT in forum New To JavaReplies: 5Last Post: 04-01-2010, 08:47 PM -
[SOLVED] java.lang.NullPointerException when reading getClass().getResourceAsStream(.
By jon80 in forum New To JavaReplies: 2Last Post: 05-31-2009, 05:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks