this means the current object, the instance we are currently running a method on. There are 5 types of variable:
If you don’t specify otherwise, Java presumes (1) local, but if it can’t find the name, it presumes (2). If you want to explicitly use (2), use the keyword this.
If you mention any instance variables in your methods without any associated pointer, e.g. height, rather than joe.height, this is automatically assumed, i.e. as if you had said this.height, meaning the height variable in the current object. Ditto for instance methods.
this has many uses:
/* constructor */ public Dog ( boolean dangerous ) { this.dangerous = dangerous; } private boolean dangerous;
/** Convenience constructor to provide defaults */ public Con ( int thing ) { this( thing, false, 0 ); }
node.setParent( this );
public Dog groomDog() { washDog(); dryDog(); return this; }
Delphi and Smalltalk programmers will recognize this as self.
If you don’t speak assembler please ignore the following. Looking at this from the point of view of an assembler programmer, for any instance method, there is a hidden first parameter on every instance method call, a pointer to the current this object that is automatically generated by the Java compiler. You can refer to this parameter by the word this inside the called method.
This page is posted |
http://mindprod.com/jgloss/this.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\this.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[3.135.193.166] |
| |
Feedback |
You are visitor number | |