6
Block Scope By Greg Butler

Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

Embed Size (px)

Citation preview

Page 1: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

Block Scope

By

Greg Butler

Page 2: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

Purpose

The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to blocks.

Page 3: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

Objectives

The student, given a short answer test, multiple choice test, or programming problem should be able to demonstrate :

• An understanding of the definition of the term block scope.

• The ability to determine a variable's scope, based on the principle of block scope.

Page 4: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

DefinitionBlock Scope

• A block is a chunk of code that begins with a { and ends with a }

• A nested block is also known as an inner block. Surprisingly, inner blocks are inside outer blocks

• The term local variable applies to those variables declared within a method

{ { }}

Outer blockAny variable delcared here can be used in any associated inner blocks

Inner blockAny variable delcared here can be used in any associated inner blocks, but not in the outer block

A variable or identifier declared within a block "can only be used within that block or in blocks nested within that block

Page 5: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

import javax.swing.*;

public class DemoBlockScope

{

static final int CONST_DEMO_SIZE = 6;

static int arrayConstants[ ] = new int [CONST_DEMO_SIZE ];

static void ReadOutArray ()

{

for (int index2 = 0; index2 < CONST_DEMO_SIZE; ++index2)

{

int tempVar = arrayConstants [index2];

System.out.println ("point 2: " + index2);

System.out.println (" Element "+ index2 + " is: " + tempVar ) ;

} // end for

} // end Read Out Array

public static void main(String args[])

{

int index = CONST_DEMO_SIZE -1;

do {

arrayConstants [index] = Integer.parseInt (JOptionPane. showInputDialog(

"What do you want in the next array element?"));

--index;

} while (index >=0); // end do-while

ReadOutArray();

} //end main

} //end class ArrayBasicsOne

Example-Block Scope

Both of these variables have class scope since they are not declared inside any of the methods within the class. They can be used in any of the blocks nested within the class (e.g. ReadOutArray( ) and main( ).

index2 is declared in the arguments section of a "for" statement. Its scope is therefore limited to use within that "for" statement and any blocks that nested in the for statement

You can't use "index2" or "tempVar " here, since you are outside the block in which they were declared.

You can use "index" here. Index was declared in main()-- the outer block. The block associated with the "do", wherin this statement appears, is nested within the main()-- a inner block.

You can't use "index" here, you are outside the main() block, where it was declared. It is local to the main() method

Page 6: Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to

Block ScopeSummary

• If the declaration of a variable occurs outside any methods or other class inner blocks, it has class scope. This means it can be used by any of the methods inside the class.

• If the declaration of a variable occurs in a method, it is a local variable of that method. It can be used in any blocks embedded in the method (its inner blocks), but not outside the method in which it was declared.

• In general, you can use a variable in any blocks nested inside the block in which the variable was declared. You cannot use in any block outside the block in which it was declared.