The concept of variable scope and local variables in java programming. It covers the visibility and accessibility of variables in different code blocks, the difference between local variables and fields, and the rules for hiding and avoiding variable name conflicts. Examples are provided to illustrate the concepts.
Download Understanding Variable Scope and Local Variables in Java and more Slides Java Programming in PDF only on Docsity!
Local Variables and Scope
Variable Scope
• A variable’s scope consists of all code blocks in
which it is visible.
• A variable is considered visible if it can be
accessed by statements within that code
block.
• Java Code block: One or more lines of code
contained between braces {}
• Code blocks can be nested
{ Statement1; {Statement2;}}
Variable Scope Example
Below, block 1 declares A. Block 2 declares B. A’s scope is
within all the blocks. B’s scope is only in block 2. Block 3
cannot access B because it is located outside of block 2’s
enclosing braces, so it will generate an error!
– Block 1 : {Var A; A = 10;
Block 2 : {Var B; B = 2 *A;}<- inside 1
Block 3 : { B = A;} }<- end of block 1
Error ^
Classes and Variable Scope
• The class code below acts the same as the
code blocks example in the previous slide.
class A {Var A1 = 10;//A1 is a field } class B extends A { // Var A1’s scope includes B Var B1 = 2 * A1; } class C extends A { // A1’s scope includes C B1 = 2 * A1;}// illegal, B1 not // in scope
Local Variables
• A method’s parameters are local to the block
of the method’s body.
public void aMethod(int A){...};
• If new variables are declared in the
initialization of a for loop, they are local to the
loop body.
for (int i = 0; i < N; i++);
• i’s scope is just the body of the loop.
Hiding fields
• If a local variable is declared with the same
name as an in scope field, that field is
“hidden” or “shadowed”.
class MyClass {int A; // field public void someMethod() { String A;// Local variable }
Hiding Fields cont.
• In the above example, in someMethod, A1 is a
local variable of type String.
• The field A1 is only accessible with the this
reference within someMethod:
A1 = “Blah”; // local A1, String
this.A1 = 5; // field A1, int
• If one of a method’s parameters have same
name as a field, also hides the field
Avoid Hiding Fields
• Hiding should generally be avoided, with the
following exceptions
– In constructors, the parameters holding values to
set fields to can have the same name as the field
– Within a class’s methods, it is ok to hide/shadow
instance fields of that class if the local variable is a
copy of the field, since readability will be
improved.
Common Mistakes
• Declaring a variable in a for loop then trying to
use it after the loop
for (int i = 0; i < size; i++){ ... //do loop stuff}System.out.println(“Count: “ + i);
• The above is incorrect, since the scope of i
ends at the end of the for loop.
• To correct: declare i before the loop
Corrected Mistake
• Correction of above example:
int i;for (i = 0; i < size; i++){ ... //do loop stuff}System.out.println(“Count: “ + i);