Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keyword "this"

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm frustrated and need to go back to basics.
Searching google doesn't help when you're looking for the keyword "this"

Can someone help me understand what using it does?
I think it is a reference back to its own class, but I'm fuzzy on the details.
 
George Franciscus
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class may have many instances. You can use "this" to obtain a reference to the current instance.

Here's an example ...

public Class MyClass {

String name;

public setName(String name) {
this.name = name; <----
}

}

At the arrow the the current instance variable "name" of this class is being assigned the method argument called "name"
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty close. The keyword "this" is a reference to the current object. I typically use it when refering to a member field or calling another method in the same class:

While this is superfluous, it helps me differentiate between member fields and local variables. In my opinion, it makes the code easier to read. However, there is a more important use: when you need to pass "this" as a parameter to a method:

Of course, the above are contrived examples to illustrate how to use "this". When you encounter situations like this (pun intended), "this" comes in handy.

Layne
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" is also very useful for registering event handlers and for adding the current object to a collection, such as a queue...
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the scale of being lost

lost|-----me------------------------|you guys

So, its like saying "take the results of object(this) with name(variable) (written as this.var) and..."?
Is it just a shortcut to saying, "as a member of this class"

I'm trying to write all of this without using the keyword this.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a Java object, you get a reference which you usually assign to a variable. For example:


Now you have a reference to the Integer object, and that reference is the value stored in the variable i.

Every object has a sort-of default reference, in which the object's reference points to itself. You can think of "this" as "me". It refers to the object, not to any part of the object, such as data members or function members. So, "this" is the equivalent of the variable "i" above, except it's for the current object and means "me". So, if an object wants to put itself in a queue for processing, it could, say:



which means




Does that help?
[ January 31, 2005: Message edited by: Jeff Bosch ]
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so when this.variable is used it means

current_object.specific_variable_of_that_object
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct!

Here's one place it can be useful:



Here, you've used "this" to remove potential ambiguity about which value is being assigned to which variable. In this case, the object's variables are being assigned the parameter values. While this may not be the best practice for constructors, it does demonstrate one use for the "this" keyword.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add one more modification to your statement:

current_object.specific_variable_of_that_object


I would change it to:

current_object.specific_member_of_that_object


because "this" can apply to methods too...
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys rock!
I knew I loved these forums for a reason.
Thanks for your patience and help.
I think I got it now. I'm gonna have to write some code and make sure I can actually use it now.

Thank you
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, you're very welcome! That's why we're here.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that there is another use for the keyword: for chaining constructors:

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember the Wicked Witch of the West after she got water thrown on her...

yeah, that's me right now.
hehe
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I heard eyes glazing over!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic