• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using the "this" keyword  RSS feed

 
Grant Olde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a "leap-year" dabbler in java (every 29th February). Each time I come back to java, I have to relearn "this"

There are many code examples on the use of "this". For example:
http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html

The problem with the examples is that they show the correct use of "this", but they don't say what would happen if you didn't use "this"

I think it might help me if someone could make a statement such as:

If you want result X, then you must use "this". But if you want result Y, then don't use "this". If you want result Z, then it doesn't matter whether or not you use "this".

Therefore, I would be most grateful if someone could give me two very short pieces of code that I could run in Eclipse.

One piece of code would use "this", and the other would not. I can then use single-stepping to examine what happens with and without "this"

I would prefer if the sample code did not refer to a constructor, so that I can get the simple stuff in my mind first (if ever!)

Of course, if some kind person wants to provide *four* tiny runnable code examples (two pieces that are not for a constructor, and two pieces for a constructor), then I would be delighted. Killing 4 birds with "this" stone
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'this' is simply a reference to the current object. Sometimes you explicitly need to refer to the current object, so you need to use 'this' - for example, if you have a method with an argument which has the same name as a member variable.

Note: Don't forget to check your private messages (click the link at the top left of the page).
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to what Jesper said, when you have anonymous inner classes, you can still use the this keyword to access the current object.
 
Grant Olde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Maneesh and Jesper

I expect to get the hang of this by 2012 (next leap year)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Old Grantonian" - Please check your private messages, as Maneesh and I asked you before.
 
Grant Olde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:"Old Grantonian" - Please check your private messages, as Maneesh and I asked you before.


Hi Jesper and Maneesh

Sorry for all the hassle about my name.

As I already mentioned to Maneesh, my first name is Oldridge. This was the name of my late mother's late father. Since my first day at school, I've been called "Old"

I get comments such as "Are you young or old, young Old?"

Will it be OK if I change my personal details from "Old" to "Oldridge", or do I need to choose a completely different fake name?

Best regards,
Old
.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!