The this Keyword

Before concluding this chapter it is necessary to introduce this. When a method is called, it is automatically passed an implicit argument that is a reference to the invoking object (that is, the object on which the method is called). This reference is called this. To understand this, first consider a program that creates a class called Pwr that computes the result of a number raised to some integer power:

Image

As you know, within a method, the other members of a class can be accessed directly, without any object or class qualification. Thus, inside get_pwr( ), the statement

means that the copy of val associated with the invoking ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.