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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly