4 Replies - 510 Views - Last Post: 23 April 2012 - 06:14 PM Rate Topic: -----

#1 jag4205  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 04-March 12

What does a "this" reference do?

Posted 23 April 2012 - 02:50 PM

I was wondering what a this reference do?

For example: this.hour = 10;
Is This A Good Question/Topic? 0
  • +

Replies To: What does a "this" reference do?

#2 Dankwansere  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 205
  • Joined: 09-November 09

Re: What does a "this" reference do?

Posted 23 April 2012 - 03:01 PM

it references the current variable in your class here's an example

public class test
{
   int num1

   public test(int num1)
   {
     this.num1 = num1
   }
}




you see in the code above I created an int variable called "num1" and also in the constructor I take an int argument also called num1. lets say whichever value the constructor gets from its argument it should assign it to the num1 variable I created in the class, if I had not put the "this" keyword the compiler would be confused as to which num i'm referring to. Because both class variable and the constructor argument have the same name.

So when I put the this keyword, i'm letting the compiler know that I am referring the "num1" variable that I created in the class and not the "num1" variable that's in the constructor argument.

If you don't want to use the this keyword then all you would have to do is give different variable names that doesn't conflict with the ones that are given in a parameter of a constructor or a method here's an example of writing the same code above but without using the "this" keyword

public class test
{
   int num1

   public test(int number)
   {
     num1 = number
   }
}




see how I changed the constructors parameter name so it doesn't conflict with the num1 variable I previously declared?
Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,096
  • Joined: 05-April 11

Re: What does a "this" reference do?

Posted 23 April 2012 - 03:26 PM

Oracle has a good explaining page on what the keyword "this" means :) Link

Within an instance method or a constructor, this is a reference to the current object the object whose method or constructor is being called.
Was This Post Helpful? 0
  • +
  • -

#4 jag4205  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 04-March 12

Re: What does a "this" reference do?

Posted 23 April 2012 - 04:11 PM

So for the example this.hour = 10;

"This" is a reference to the object hour. Is this right?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7736
  • View blog
  • Posts: 13,068
  • Joined: 19-March 11

Re: What does a "this" reference do?

Posted 23 April 2012 - 06:14 PM

No - in this example "this" is a reference to whatever instance this code is being executed in. "hour" is a reference to "hour".


Look at Dankwansere's example again, it's a good one.
I'll expand on it a bit:

public class Test
{
   int num1

   public Test(int num1)
   {
     this.num1 = num1
   }
}



public class TestRunner
{
  Test test1  = new Test(3);
  Test test2 = new Test(7);

  public static void main (String[] args)
  { 
      //   blah blah
  }

}


In this case, there are two instances of Test created in TestRunner. At this line


  Test test1  = new Test(3);


we call the constructor. When that constructor runs, "this" refers to the object we've named as "test1". In the second line, when the constructor runs, "this" refers to the object we're calling test2.

You can test this by creating this class and giving it a getter method to get the num1 object.

test1.getNum1();


will return 3

and

test2.getNum1();


will return 7.

More clear now?


EDIT: Musing on this, I find that "this" is a little bit deceptive because what it generally means is somewhat the opposite of our normal English usage. In normal English, "this" means "the one nearest to hand". "This apple" is the one I have in my hand, "that apple" is the one over there on the table. Now, in Java, the one nearest to hand is the one you use automatically, so "this" means actually "not the one here in my hand". That's awkward! Fortunately, it's a confusion that you're not going to run into because by the time you understand that you ought to be confused by it, you understand it.

In Dankwansere's example quoted above, there are two things called num1 in the the Test class. One is a field of the class, and it persists with the object as long as the object persists. The one declared within the constructor (as an argument to the constructor, but it's lexically within the constructor) is a completely different object, and it only exists as long as the constructor is running. Nobody else can ever look at that particular entity.
When he says this.num1=num1, he means "the num1 that pertains to the current oject (not the one in the current scope) is to be set equal to the num1 that is in the current scope".

This may clear things up, or it may confuse you more. It's true all the same. :)

This post has been edited by jon.kiparsky: 23 April 2012 - 06:35 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1