Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: Java getClass()

  1. #1
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Java getClass()

    This is an example about loading resource file:
    Java Code:
    private URL getURL (String filename) {
            URL url=null;
            try{
                url=this.getClass().getResource(filename);
            }
            catch (Exception e){}
            return url;
        }
    The thing I don't understand is: getResource() is a method of getClass, right ? So. I think above line should be: url=this.getClass.getResource(filename) (no () )
    Who can explain for me why, please.
    thanks :)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Java getClass()

    That's called method chaining.
    So methodA() returns an object of ClassA. ClassA has a methodB(), so you can call that directly without assigning to a local variable.
    Java Code:
    ...
    methodA().methodB();
    // is the equivalent to
    ClassA a = methodA();
    a.methodB();
    ...
    getClass() returns a Class object on which you can call the getResource() method.
    hqt likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Java getClass()

    Quote Originally Posted by hqt View Post
    The thing I don't understand is: getResource() is a method of getClass, right ?
    Wrong. It's a method of java.lang.Class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. using getField method of getClass
    By jdigger in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 11:16 PM
  2. 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
  •