Results 1 to 6 of 6

Thread: "this"

  1. #1
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default "this"

    Can someone explain to me the use of the word "this"? I used it in JavaScript to refer to a particular object, but I am not too clear of its use in Java.

  2. #2
    Join Date
    Sep 2008
    Location
    London or Slovakia
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    Have look at this tutorial it should make it clear for you

  3. The Following User Says Thank You to peter_budo For This Useful Post:

    xtiano77 (10-07-2008)

  4. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    'this' refers to the instance on which the method is currently being called. For example, if you have:
    Code:
    class Foo {
      public string baz;
    
      public String bar() {
        return this.baz;
      }
    
      public static void main(String[] args) {
        Foo f = new Foo();
        Foo.baz = "fish";
        System.out.println((new Foo()).bar()); // Will print 'fish'.
      }
    }
    It serves roughly the same function in Java as in Javascript, but has vastly different mechanics due to the differences between the two languages.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. The Following User Says Thank You to Twey For This Useful Post:

    xtiano77 (09-27-2008)

  6. #4
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    I understand the purpose it it, but I am still not very clear. I guess I'll have to keep on reading until the clouds disipate and the light breaks through. Thanks.

  7. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This article I wrote might help you, Javascript-oriented though it be: http://fn-js.info/faqs/context. The same principle applies to Java, although methods are not truly properties in Java, since it doesn't have first-class functions.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. The Following User Says Thank You to Twey For This Useful Post:

    xtiano77 (10-07-2008)

  9. #6
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    I finally got it. Thanks Twey and everyone for your help. I guess I need to relax and not streess over it so much and eventually the clouds will disipate and sunlight will shine through (figuratively speaking). Thanks again.

Bookmarks

Posting Permissions

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