• Post Reply Bookmark Topic Watch Topic
  • New Topic

'this' question  RSS feed

 
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
Sheriff
Posts: 15861
81
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: 53774
128
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!