local variables and scope n.
Skip this Video
Download Presentation
Local Variables and Scope

Loading in 2 Seconds...

play fullscreen
1 / 16

Local Variables and Scope - PowerPoint PPT Presentation

  • Uploaded on

Local Variables and Scope. Benjamin Fein. 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 {}

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Local Variables and Scope' - jun

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
variable 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 cont
Variable Scope cont.
  • What is a variable’s scope?
    • Starts at the declaration statement
    • Ends at the end of the block it was declared in
  • Field: Variables declared in a class declaration block, outside of methods or constructors
  • A field’s scope is the whole class. The field must be declared before any initialization block, but can be after method declaration.
variable scope example
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 1Block 3:{B= A;}}<-end of block 1

Error ^

classes and variable scope
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 CB1 = 2 * A1;}// illegal, B1 not // in scope
local variables
Local Variables
  • A code block’s local variables are those declared in the code block.
    • NOT variables from higher code blocks: A in above example is local variable for block 1 only
    • NOT variables declared in nested sub-blocks: B in above example is NOT local to block 1.
  • A class’s fields are not considered local variables: they are member variables
local variables1
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
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 fields1
Hiding Fields
  • If a sub-class declares a field with the same name as one of the inherited fields of its parent, the parent’s field is hidden
    • class MyClass { int A;}class SubClass extends MyClass { int A;}
hiding fields cont
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
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.
hiding variables
Hiding Variables?
  • Variables, unlike fields, cannot be hidden
    • public void someMethod(int B) { int A; { int A; // illegal statement int B; // illegal statement } }
common mistakes
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
Corrected Mistake
  • Correction of above example:
    • int i;for (i = 0; i < size; i++) { ... //do loop stuff}System.out.println(“Count: “ + i);
  • A variable’s scope starts at declaration, extends to nested blocks, and ends at the end of the block it was declared in.
  • A blocks local variables are variables declared within that block of code, but do not include variables declared in nested blocks.
  • A class’s fields can be hidden by local variables or method parameters, but this is generally a bad idea unless the variable is a copy.
  • Jia, Xiaoping. Object-Oriented Software Development Using Java. 2nd ed. New York: Pearson/Addison Wesley, 2003. p 116-117.
  • Scope. Sun Microsystems. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html
  • Sebesta, Robert W. Concepts of Programming Languages. 7th ed. New York: Pearson/Addison Wesly, 2006. p 228-234.