• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is 'this'?

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gone through some books and document but can't understood concept of 'this'. Can somebody elaborate the meaning and concept of 'this'. I got in mess with concept and use of 'this'. Please help me to understand following..

1) What is 'this'? Why it is used in language?
2) How much it is beneficial?
3) Where it is must?
4) Role of 'this' in threading?
5) A real life example to imagine the concept and importance of 'this'.

Thanks to all in advance.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do a search, you will find this comes up frequently. Several old threads: 1 2 3 and one example where I think "this" has been used inappropriately here.

Read those, and see whether they help; if not please ask again
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1-3, 5 - Using the this Keyword
 
Dennis Korbar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "this" reference is a reference to the current object, if you use the "this" reference inside of a method or a constructor it will point to the actual object that was used to invoke this method.



It can be used to access member fields in case they are hidden/concealed (not sure what the actual English term for it is...) by local fields.
You can also use it to pass a reference to the current object to a method.

You could also have a look at the following url: http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html

 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Korbar wrote:It can be used to access member fields in case they are hidden/concealed (not sure what the actual English term for it is...) by local fields.
I think (you can read it here in the Java Language Specification if you are feeling brave) the term is "shadowed."
 
Dennis Korbar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadowed eh? There were quite a lot of translations for "verdeckt" on http://dict.leo.org but shadowed was not amongst them :-)
Thanks for the information!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. I think shadowed is correct. Remember you are handicapped using dict.leo. It only translates into normal English :P
 
kelly devon
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" refers to a current page you are working with. instead of you to call the file by name you simply refer to it by "this" helping to reduce tutology or errors by mispelling by mistake. this can also be used when you are writting threads.the role of "this" in threading is just that when you have many threads going on you can easily refer to the latest thread by referring to "this" instead of causing you confusion on the name of the particular thread you are working with
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kelly devon wrote:"this" refers to a current page you are working with. instead of you to call the file by name you simply refer to it by "this" helping to reduce tutology or errors by mispelling by mistake. this can also be used when you are writting threads.the role of "this" in threading is just that when you have many threads going on you can easily refer to the latest thread by referring to "this" instead of causing you confusion on the name of the particular thread you are working with
Page? File? You mean an instance of a class, surely?

And what's tutology? How do you get a thread with the "this" keyword?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kelly devon wrote:"this" refers to a current page you are working with. instead of you to call the file by name you simply refer to it by "this" helping to reduce tutology or errors by mispelling by mistake. this can also be used when you are writting threads.the role of "this" in threading is just that when you have many threads going on you can easily refer to the latest thread by referring to "this" instead of causing you confusion on the name of the particular thread you are working with


Hogwash. There are no "pages" in Java, and the object to which "this" refers can't be referred to in any other way, so misspelling is not a danger. Furthermore, "this" has no special role with respect to threads in Java; as always, "this" refers to the object on which the method containing the executing code was invoked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic