Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this keyword. optional or compulsive

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is usage of this keyword in JAVA optional or Compulsive?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's only required if there would be a naming conflict. The most often occurring example of that is in setters:
Without the "this.", the statement would be an identity assignment of the parameter field to itself.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that "this" is also used within a constructor to call an overloaded version of that constructor. (See Java Tutorials - this.)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I think you meant compulsory, not compulsive.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:And I think you meant compulsory, not compulsive.

Never seen code where this is used at every opportunity?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic