Java Glossary : this

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : T words : this.

this
this means the current object, the instance we are currently running a method on.

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:

  1. Sometimes you might choose to use an explicit this just to make it clear what you are up to, that you are not using a static or local variable.

  2. this is most useful in constructors and get/set accessor functions where you have a parameter with the same name as an instance variable. this.dangerous refers to the instance variable, whereas plain dangerous refers to the parameter. Be careful using this idiom. You can easily misspell the parameter, and the compiler will not warn you.

    /* constructor */
    public Dog ( boolean dangerous )
       {
       this.dangerous = dangerous;
       }
    private boolean dangerous;
    

  3. this is also often used in convenience constructors to provide default values. In that context this means call another constructor of the same class.

    /** Convenience constructor to provide defaults
     */
    public Con ( int thing )
       {
       this( thing, false, 0 );
       }
    

  4. Often you need to refer to the current object when you want to pass a reference to it as a parameter, e.g. to say the current object is the child or parent of some other object.

    node.setParent( this );
    

  5. For convenience you sometimes want a method that does something to an object then returns a reference to it.

    public Dog groomDog()
       {
       washDog();
       dryDog();
       return this;
       }
    

  6. Beware, inside an (anonymous) inner class, this refers to the inner class, not the usual enclosing one. If the outer class were called MyOuter, you could get at its this with MyOuter.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 method call, a pointer to the object automatically generated by the Java compiler. You can refer to this parameter by the word this inside the called method.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1926.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/this.html J:\mindprod\jgloss\this.html