this : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

this

this means the current object, the instance we are currently running a method on. There are 5 types of variable:

  1. Local in the current method in the current thread
  2. instance in the current object.
  3. instance in some other object.
  4. static in the current class.
  5. static in some other class.

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.

Uses for this

this has many uses:

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.

Inside an inner class or inner anonymous class this.x() refers to the innermost class. However, if you leave off the this. it could also possibly refer to any of the enclosing classes as well.

This Class

Java has no keyword corresponding to this-class, though you can get at the current class

Learning More


This page is posted
on the web at:

http://mindprod.com/jgloss/this.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\this.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[54.159.231.135]
You are visitor number