• Post Reply Bookmark Topic Watch Topic
  • New Topic

The this keyword for real greenhorns  RSS feed

 
Keith Alexander
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to understand the use of the 'this' keyword. I've followed my book examples and examples given online but when I take out the 'this' from the example code, the results are the same so I'm simply not understanding what 'this' is doing.

e.g.


Question: can anyone send me a link to a good, greenhorn readable, explanation of what 'this' is doing and how it is used. I.e. an example to show me different behavior with and without it. or a simple piece of code as an example of different behavior with and without 'this'.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this references the instance of the object.

Sometimes, it's optional. For example, in lines 6 and 10, you could omit this. and the member variable would be automatically assumed to belong to the current instance.

It's only absolutely necessary when the reference could be ambiguous, but many developers, myself included, like to use it as a prefix for all references to members as it makes the intent of the code crystal clear.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of when it is necessary:



In line 6, the this. prefix is required as without it, just fred refers to the parameter named fred, not the member variable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another use of this is when you need to pass a reference to the current instance to a method of another class:

 
Keith Alexander
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll be honest, it's not sinking in ... I think I need to move on, get some more practice experience and maybe come back to this concept when I can better understand/see it in action. Thanks - it almost makes sense but I don't truely appreciate it's value just yet. (Very frustrating but I'm a greenhorn ... so time ...).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, just keep in mind that when you create a method on a class, that method can run for any and all instances created of that class. The this keyword is the way that a method can reference the instance that it "belongs" to among all of the created instances.
 
Keith Alexander
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, got it! That finally made sense (all the code examples were not clicking with me but that now makes sense). Thanks Bear.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help! Sometimes it just takes a few approaches before finding the one that clicks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!