Unformatted text preview:

Local Variables and ScopeVariable ScopeVariable Scope cont.Variable Scope ExampleClasses and Variable ScopeLocal VariablesSlide 7Hiding fieldsHiding FieldsHiding Fields cont.Avoid Hiding FieldsHiding Variables?Common MistakesCorrected MistakeConclusionBibliographyLocal Variables and ScopeBenjamin FeinVariable 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.•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•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•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 BVar B1 = 2 * A1;} class C extends A {// A1’s scope includes CB1 = 2 * A1;}// illegal, B1 not // in scopeLocal 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 variablesLocal 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; // fieldpublic void someMethod() { String A;// Local variable }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.•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 fieldAvoid 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?•Variables, unlike fields, cannot be hidden–public void someMethod(int B) {int A;{ int A; // illegal statement int B; // illegal statement }}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 loopCorrected Mistake•Correction of above example:–int i;for (i = 0; i < size; i++) { ... //do loop stuff}System.out.println(“Count: “ + i);Conclusion•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.Bibliography•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


View Full Document

NJIT CS 602 - Local Variables and Scope

Download Local Variables and Scope
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Local Variables and Scope and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Local Variables and Scope 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?