ControlledLocalAccess
One can control the level of access to local variables by adding access modifiers
(like public
or private
) to local variable declarations.
One can control access to local variables using access modifiers
One cannot control access to local variables
CorrectionHere is what's right.
Local variables can never be accessed outside the method in which they are declared.
Thus it makes no sense to provide access modifiers like public
or private
.
In fact, it is a compiler error to use such modifiers in local variable declarations.
Note for the advanced: If a method contains an inner class, then its (effectively final) local variables may be read from within code in the inner class.
SymptomsHow do you know your students might have this misconception?
This misconception is directly visible in code. Here is an example occurrence from a Java course.
public class C {
public int m(int[] vs) {
public int sum; // error
for (int v : vs) {
sum += v;
}
return sum;
}
}
It may be hard to spot the misconceptions, because a compiler or syntax-directed IDE will immediately flag the problem, and thus students may correct it without truly understanding.