• 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

Please explain this line of code to me

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had my program edited by some guy, and he put in the following lines of code:



How come I have to add this.* to the bold lines? Everything else works just fine without it.

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

Originally posted by Klaas Vredevort:
I had my program edited by some guy, and he put in the following lines of code:



How come I have to add this.* to the bold lines? Everything else works just fine without it.

Klaas





Your code might be inside a method, if i am not wrong. because your post is incomplete.

Any way, What i think , there is a variable named wordToPlayWith inside the method, which is local variable. And you also have a class variable with same name wordToPlayWith.

To remove the name conflict we use this . using this.wordToPlayWith refers to your class variable and not your local variable
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Sunil Kumar Gupta pointed out above, we would need to know more about the context to say for sure. However, Bruce Eckel notes that...

Some people will obsessively put this in front of every ... field reference, arguing that it makes it "clearer and more explicit." Don't do it. There's a reason that we use high-level languages: They do things for us. If you put this in when it's not necessary, you will confuse and annoy everyone who reads your code...


(Footnote on page 169 of Thinking in Java, 4th edition.)
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic