• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain this line of code to me

 
Klaas Vredevort
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic