Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'this' question

 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If using a class attribute, is it in good practice to use "this" i.e.

 
lil Yaure
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally hate the this word, I always use _prefix instead for private variables

"this" word makes the code too colorful in netbeans, but thats just my personal preference
Many people use "this" though
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*this* refers Currently executing Object. Typically it is use to avoid the Shadowing of the Variable.

example:



in above case , in setter if you use email = email then the value assigned to itself still instance variable (email) is null . so to point the instance , we use this .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a matter of style.

Personally, I only use 'this' when necessary, I don't use it on any access to a member variable. I also don't use '_' or any other prefix for member variables (that looks ugly, in my opinion).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will sometimes want to send the whole object, in which case you need the this keyword. Note also the use of this() when overloading constructors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic