- Learning Java by Building Android Games
- John Horton
- 175字
- 2021-07-23 19:02:33
Using "this"
When we use this
as we do in the previous lines of code we are referring to the instance of the class itself. By using the this
keyword it is clear when we mean the member variable or the parameter.
As another common example, many things in Android require a reference to an instance of Activity
to do its job. We will fairly regularly, throughout this book pass in this
(a reference to the Activity) in order to help another class/object from the Android API do its work. We will also write classes that need this
as an argument in one or more of its methods. So, we will see how to handle this
when it is passed in as well.
There are more twists and turns to be learned about variables and this
and they make much more sense when applied to a practical project. In the next mini-app, we will explore all we have learned so far in this chapter and some more new ideas too.
First a bit more OOP.