Results 1 to 2 of 2
  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default What does it mean to reference the current object?

    The keyword "this" references the current object. What does that mean? Thank you.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What does it mean to reference the current object?

    So when we write a java class, and instantiate that class as an object, the code executing inside said object and reference itself using the keyword this. It is a common idiom when working with constructors or setter methods, but can be used all over the place.

    For example:

    Java Code:
    public class Alpha{
        int a;
    
        public Alpha(int a){
            this.a = a;
        }
    }
    In this example, we have a constructor that takes a parameter called 'a' which coincidentally is also the name of the declared instance variable above it. When we say "this.a = a;" we're literally saying "the 'a' belonging to the Alpha instance we are currently in should be assigned the value contained in the parameter named 'a'".

    You'll also see it used quite a bit when working with actionListeners and GUIs - frequently one will implement an interface and then pass a reference to the current class (this) to a method that assigns an action listener.

Similar Threads

  1. Reference of an object
    By saiganesh44 in forum New To Java
    Replies: 7
    Last Post: 04-21-2012, 12:22 PM
  2. What to do with object reference?
    By kyle_maddisson in forum New To Java
    Replies: 6
    Last Post: 11-04-2011, 06:58 AM
  3. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 09:39 PM
  4. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 03:51 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
  •