*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this keyword. optional or compulsive" Watch "this keyword. optional or compulsive" New topic
Author

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

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
sscce.org
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

And I think you meant compulsory, not compulsive.


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

Joined: May 03, 2008
Posts: 4523
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: this keyword. optional or compulsive
 
Similar Threads
create jar problem: jarFile.jar: no such file or directory
WA #1.....word association
finally null variables
interface
outer join in DB2