wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes 'this' question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Watch " New topic
Author

'this' question

Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

If using a class attribute, is it in good practice to use "this" i.e.



star chaser..
lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
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

Joined: Jan 28, 2008
Posts: 5575

*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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'this' question
 
Similar Threads
Netscape7.1 and window.createPopup()
Why we live?
onload
input type=file
build.xml question