• 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

when and how to use this keyword in a method

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



What is the point of the this keyword in this example from Bates and Sierra's book? I tried the example both with and without it and got the same result. So now I'm confused about when to use this inside a method. Can someone give me an example that actually makes a difference in the output?

Thanks, Vonique
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read Using the this Keyword, it contains good explanation on usage of this with some examples.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again, VL.

There is a 3rd use of this, which that link misses out. It can mean the whole object you happen to be in. You cna have a serialising method which writes the entire object to a file with an object output stream or similar:-
oos.writeObject(this);
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The keyword can also help you access same-named variables that have been shadowed in the current scope. In the example below, the 'message' field declared on line 3 is shadowed by the parameter on line 10. Using 'this' on line 11 allows you to access the field instead of the parameter. I think this is kind of where the example you cited was going. Note that this is just an example and you should avoid situations where names are shadowed in real-world code.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my above example the this keyword doesn't seem necessary because if you take it out you get the same results. Why would the writers include it? And also, I understand that this.coolMethod() refers to the current object that is calling it so it returns "Wow baby" but can anyone explain in which situation would it return something different? I guess that is what I am struggling to understand.

Hi again to you Campbell!
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I think I just figured out my answer. If I alter the code like this:



and override the method, then the second coolMethod() inside Moo returns "Hi baby."

Got it. The little snippet in the SCJP book uses this.coolMethod() when it wasn't even necessary and that is what threw me off.

Thanks, all

Vonique
reply
    Bookmark Topic Watch Topic
  • New Topic