This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Watch " New topic

'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

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.


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: 14116

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

Joined: Oct 13, 2005
Posts: 38509
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.
subject: 'this' question