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 keyword. optional or compulsive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this keyword. optional or compulsive" Watch "this keyword. optional or compulsive" New topic

this keyword. optional or compulsive

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Is usage of this keyword in JAVA optional or Compulsive?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19911

It's only required if there would be a naming conflict. The most often occurring example of that is in setters:
Without the "this.", the statement would be an identity assignment of the parameter field to itself.

How To Ask Questions How To Answer Questions
marc weber

Joined: Aug 31, 2004
Posts: 11343

Note that "this" is also used within a constructor to call an overloaded version of that constructor. (See Java Tutorials - this.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62139

And I think you meant compulsory, not compulsive.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darryl Burke

Joined: May 03, 2008
Posts: 4761

Bear Bibeault wrote:And I think you meant compulsory, not compulsive.

Never seen code where this is used at every opportunity?

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: this keyword. optional or compulsive