8.2-2_Accessing the this Reference Flashcards
1
Q
What happens when a method parameter has the same name as an existing instance variable?
A
Java uses the narrowest/most granular scope it can
2
Q
What does the ‘this’ keyword means?
A
It refers to the current instance of the class and can be used to access any member of the class. ✅ It can be used in any instance method, constructor, and instance initializer block. ❌ It cannot be used when there is no implicit instance of the class, such as in a static method or static initializer block.
3
Q
What are the most common ‘this’ reference/keyword pitfalls?
A
- > Be aware of backwards assignment. color = this.color;
- > Notice that when there is no naming collision, ‘this’ keyword is not required.
- > Variable assignment is allowed to use this even when there is no duplication of variable names.