• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is 'this'?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1-3, 5 - Using the this Keyword
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic