Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: the word "this"

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default the word "this"

    I see the term, "this" used here and there and I don't understand what it means. I have searched around and has been said it means the current item. Well, I understand current item when using a cursor traversing through a linked list and how it stops at the current item. maybe that's it? Even if I'm hitting the nail on the head here, I don't understand how "this" is used. I have seen "this" used in two ways.

    one way was something like this: this.xyz = xyz;

    another way I am seeing it now in my book in the binary trees chapter is this:
    {
    right = right.removeRightmost( );
    return this;
    }
    Last edited by helloworld922; February 26th, 2010 at 02:07 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: the word "this"

    This refers to the current object you're method is working with. Note that this can only be referenced in a non-static method (otherwise, there is no object you're working with)

    public static void main(String[] args)
    {
         System.out.println(this.toString()); // Syntax error: We're inside a static method right now
    }

    It is often used to clear-up which field/variable you are referring to because by default, local variables will cover up instance variables and static fields.

    public Class A
    {
         int number;
         public A(int number)
         {
              this.number = 5; // The field number
              System.out.println("This number is " + this.number);
              System.out.println("The local number is " + number);
         }
     
     
         public static void main(String[] args)
         {
              new A(3);
         }
    }

Similar Threads

  1. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  2. Reserved word "throws"
    By Lil_Aziz1 in forum Exceptions
    Replies: 1
    Last Post: January 1st, 2010, 01:12 AM
  3. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM