Newer Version Available

This content describes an older version of this product. View Latest

Using the this Keyword

There are two different ways of using the this keyword.

You can use the this keyword in dot notation, without parenthesis, to represent the current instance of the class in which it appears. Use this form of the this keyword to access instance variables and methods. For example:

In the above example, the class myTestThis declares an instance variable s. The initialization code populates the variable using the this keyword.

Or you can use the this keyword to do constructor chaining, that is, in one constructor, call another constructor. In this format, use the this keyword with parentheses. For example:

When you use the this keyword in a constructor to do constructor chaining, it must be the first statement in the constructor.