this : Java Glossary

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.

This Class

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

Learning More


CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/this.html J:\mindprod\jgloss\this.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.213]
You are visitor number 25,665.