UNIT 3: CLASS CREATION · TOPIC 3.8
3.8 Scope and Access
Where a variable can be used depends on where it was declared. Local variables shadow instance variables, and blocks end scope.
What you need to know
- Instance variables are in scope throughout the class's methods and constructors.
- Local variables (declared inside a method, including parameters) exist only within the block where they're declared — from the declaration to the closing brace.
- A variable declared inside a loop body or if block is gone after that block. A for loop's header variable is gone after the loop.
- Shadowing: a local variable or parameter with the same name as an instance variable hides it. Inside that method, the name refers to the local. Use
this.name(3.9) to reach the instance variable. - Two local variables with the same name in the same scope is a compile error. In separate scopes (two different methods) it's fine.
- Access modifiers control visibility across classes:
private= this class only,public= everywhere. - A method's parameter is a local variable initialized by the argument.
Worked example
public class Demo
{
private int x = 10; // instance variable
public void run(int x) // parameter shadows instance x
{
System.out.println(x); // prints the parameter
int y = 5;
if (x > 0)
{
int z = 1; // z only exists in this block
y += z;
}
// System.out.println(z); // ERROR: z out of scope
System.out.println(y); // 6
}
}
Exam tip: Find the closing brace of the block a variable was declared in — that's the end of its life. If a question uses a variable after that brace, it's a compile error. For shadowing, the closest declaration wins.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Scope by declaration site: instance variable → the whole class. Method parameter → the whole method. Local variable → from its declaration to the end of the enclosing block
{ }. For-header variable → the loop only. - Blocks nest scope: a variable declared in an if block is gone after the if. A variable declared in a loop body is recreated each iteration and gone after. Declaring a variable inside a loop and reading it after the loop is a compile error the exam uses often.
- Shadowing: when a local or parameter has the same name as an instance variable, the local wins inside its scope. The instance variable is hidden, not changed.
this.namereaches it (3.9). - No redeclaration in the same scope:
int x = 1; int x = 2;in one method is a compile error. But two different methods can each have their ownx, and a for loop can reuseiafter a previous for loop ended. - Access (public/private) is about visibility between classes. Scope is about visibility within code. Both determine whether a name can be used at a given point, but they're different rules.
- Lifetime follows scope: a local variable's memory is released when its block ends. Instance variables live as long as the object.
- Reading a question: for every variable use, find the declaration. If it's outside the current block or hasn't happened yet, compile error.
Mistakes that cost points
- Using a loop-body variable after the loop. Out of scope.
- Using a for-header variable after the loop. Out of scope.
- Assuming a shadowed instance variable was changed. The local was changed. The field wasn't.
- Redeclaring a variable in the same scope. Compile error.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following will cause a compile-time error?
Show answer
Answer: C. sq is declared inside the loop body and is out of scope after it.
Q2 A class has
private int value = 8; and the method public int get(int value) { return value; }. What does obj.get(3) return?Show answer
Answer: B. The parameter shadows the instance variable, so value refers to the argument 3.
Key vocabulary
- Scope
- the region of code in which a variable is accessible
- Local variable
- a variable declared inside a method or block
- Shadowing
- a local variable or parameter hiding an instance variable of the same name