| Author |
What is 'this'?
|
Rahul Shilpakar
Ranch Hand
Joined: Aug 29, 2006
Posts: 132
|
|
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.
|
Perform for today. Adapt for tomorrow.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Nov 14, 2008
Posts: 67
|
|
1-3, 5 - Using the this Keyword
|
 |
Dennis Korbar
Greenhorn
Joined: Jan 14, 2009
Posts: 20
|
|
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
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Jan 14, 2009
Posts: 20
|
|
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
Joined: Oct 13, 2005
Posts: 32708
|
|
|
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
Joined: Apr 15, 2004
Posts: 71
|
|
|
"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
|
kel
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Jul 08, 2003
Posts: 24057
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: What is 'this'?
|
|
|