Study Notes on Scope, Access, and the `this` Keyword in Java
Scope and Access & this Keyword
Overview of Concepts
This section focuses on important programming concepts within Java: Scope and access of variables, and the
thiskeyword.
Big Ideas
Variables only exist within the scope where they are declared.
In cases where a parameter and an instance variable share the same name, the parameter will hide the instance variable.
The keyword
thisrefers to the current object, allowing access to its instance variables.
Types of Variables in Java
Local Variables, Parameters, and Instance Variables
Java methods can access three types of variables:
Instance Variables: These belong to the object and can be used by all methods of that object.
Parameters: These are values passed into a method when it is called.
Local Variables: These are declared inside a method and are only accessible within that method.
Handling Name Conflicts
Variable Shadowing
When a parameter shares the same name as an instance variable, the parameter hides the instance variable.
Example Consideration: In the method
setAge, when referring toage, it is referencing the parameter instead of the instance variable.
The this Keyword
Fixing Naming Conflicts with this
The
thiskeyword allows a method to refer to the current object and access its instance variables.Explanation: It clarifies that the
ageattribute is the instance variable of the object as opposed to the parameterage. This alleviates ambiguity when the names are similar.
this in Constructors
Constructors commonly utilize
thiswhen parameters match instance variable names.Purpose of Using
this:Sets the initial state of the object accurately.
Avoids conflicts in variable names between parameters and instance variables.
Key Rule for Variable Scope
Java searches for variables in the following order:
Local Variables / Parameters
Instance Variables
Clarification: The value of
ageprinted in a method will be the parameter's value, not the instance variable's, if they share the same name.
Practice with AP-Style Questions
AP-Style Multiple Choice Scenario 1
Question: What happens when
setGrade(90)is called?A. The instance variable becomes 90
B. The instance variable remains unchanged
C. The program does not compile
D. Both variables change to 90
E. A runtime error occurs
AP-Style Multiple Choice Scenario 2 (Trap Question)
Question: Given a certain code, what will be printed?
A. 0
B. 95
C. The code will not compile
D. A runtime error will occur
E. The output is unpredictable
AP Takeaways
Parameters and local variables can obscure instance variables with matching names (the latter are hidden).
The expression
this.variablerefers to the instance variable that belongs to the current object instance.Constructors frequently utilize
thisfor assigning values from parameters to instance variables.The expression
age = agedoes not alter the object's instance variableagedue to naming conflict.The principles regarding variable scope and the
thiskeyword are especially relevant when working on AP multiple-choice tracing questions, as they require careful analysis of variable hiding and scope.