File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Watch " New topic

'this' question

Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 161

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

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 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
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:
subject: 'this' question
It's not a secret anymore!