Results 1 to 12 of 12
  1. #1
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default getClass().getResource()

    Hello Everyone
    ImageIcon(getClass().getResource("exit.png"));
    Kindly explain how the above line of code does what it does.ImageIcon class has no constructors of such kind.I am really confused.Really would appreciate the help.
    Thank You

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  3. #3
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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:

    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();
        }
    }
    The printf() is just showing off (but it *is* a useful method):

    % 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.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: getClass().getResource()

    Maybe this will help you understand. The expression

    Java Code:
    getClass().getResource("exit.png");
    is equivalent to

    Java Code:
    Class klass = getClass();
    klass.getResource("exit.png");
    except that in the first case, there isn't a named variable holding the value returned by getClass().
    Get in the habit of using standard Java naming conventions!

  6. #6
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: getClass().getResource()

    So in this case it is returning the object that invokes the class?

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  8. #8
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  10. #10
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default 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

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  12. #12
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: getClass().getResource()

    Thank You for clearing such a big doubt of mine.

Similar Threads

  1. Java getClass()
    By hqt in forum New To Java
    Replies: 2
    Last Post: 01-31-2012, 08:44 PM
  2. Replies: 1
    Last Post: 10-27-2011, 10:13 AM
  3. using getField method of getClass
    By jdigger in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 11:16 PM
  4. ClassLoader getResource Problem
    By sh4dyPT in forum New To Java
    Replies: 5
    Last Post: 04-01-2010, 08:47 PM
  5. Replies: 2
    Last Post: 05-31-2009, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •