This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "keyword "this"" Watch "keyword "this"" New topic
Author

keyword "this"

Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
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

Joined: Jan 25, 2005
Posts: 32
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"


George Franciscus<br />george.franciscus@nexcel.ca<br /><a href="http://www.nexcel.ca" target="_blank" rel="nofollow">http://www.nexcel.ca</a><br /><a href="http://www.manning.com/franciscus" target="_blank" rel="nofollow">http://www.manning.com/franciscus</a>
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
"this" is also very useful for registering event handlers and for adding the current object to a collection, such as a queue...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
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

Joined: Jul 30, 2003
Posts: 804
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

Joined: Oct 27, 2004
Posts: 158
so when this.variable is used it means

current_object.specific_variable_of_that_object
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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

Joined: Jul 30, 2003
Posts: 804
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

Joined: Oct 27, 2004
Posts: 158
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

Joined: Jul 30, 2003
Posts: 804
Hey, you're very welcome! That's why we're here.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Notice that there is another use for the keyword: for chaining constructors:



The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
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

Joined: Jul 30, 2003
Posts: 804
I thought I heard eyes glazing over!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keyword "this"
 
Similar Threads
keywords and reserve words
The keyword 'this'
pending assignments
what did i break? "no main method found"
WA #1.....word association