CODE BLOCK AND VARIABLE SCOPE

During the last two posts, we looked at Java Statements where I explained the different types of statement and how they are categorized.  We have also in previous posts been familiar with declarations of different Java keyword and we have seen their various syntax. So today, I will be shifting our attention to one of the things we have seen often in my sample codes, this is the Code Block. Block of codes in programming are similar in context to paragraphs in the English language. The illustration below explains this in more detail.

ENGjAVA.PNG

CODE BLOCK

A block of code in Java is identified by a pair of curly brackets {}. It is the chunk of code between the brackets. They are used to mark the start and the end of codes that work together as a piece therefore it is safe to call the opening curly bracket the Begin statement and the closing curly bracket the End statement. There are several curly brackets in Java source file and most of the time they are found nested since they are required in  most declarations i.e. class, method, constructor, interface, methods and loop declaration. Click here to see previous post on their declaration syntax.

 See examples below: The colours differentiate the different code blocks nested together

NestedCodeBlock

Because of this nesting possibilities, it is important to keep track of every opening and closing curly brackets in order to ensure that (i) the number of opening curly brackets matches the number of closing curly brackets and (ii) they are matched together appropriately. If any of the opening curly bracket does not have a corresponding closing curly bracket or vice versa, the compiler will flag an error and the code will not compile.

See sample code below: When a closing curly bracket is missing as shown below, the error “error: reached end of file while parsing” is thrown at compilation.

NestedError

VARIABLE SCOPE

Code blocks are used to restrict variable scope. The scope of a variable is defined depending on the code block where the variable is declared such that the higher the hierarchy, the larger the scope and vice versa.

variableScope1

Member Variable Scope: A variable defined within a class or its constructor is visible within all code block in the class. These variables are the instance or object variables and class variable depending on if the keyword static is added to their declaration. See my previous post on variables here. Whether the variable is an instance/object variable or Class variable, the scope is the same such that they are visible and accessible throughout the class. On line 14 in the sample code above, Variable classVariable declared is a member variable hence it is accessible within all the blocks.

Method Parameter Scope: A method parameter is accessible throughout the method. On line 20 in the sample code above, Variable methodparam is a method parameter hence it is accessible within the method but not outside.

Local Variable Scope: Variables defined within a method are called local variables and are only accessible within the code block where they are defined within the method. On line 14 in the sample code above, Variable localVariable is a local variable hence it is accessible within the code block where it is defined.

NOTE: Local variable are not guaranteed to always be accessible throughout the method as they might be declared within a method’s child block. However, method parameter are always guaranteed to be accessible throughout the method. See example below

OutOfScope

 In the code snippet above, we can see an error flagged on line 34. That is what happens when the compiler tries to access a variable that is out of scope. This is to say that the variable “localVariable” is no longer accessible since it was declared within try block and the block has been closed on line 29.

Loop Variable Scope: Variables defined within a loop are only accessible within the loop. On line 25, the variable “i” declared and initialized within the for loop is only accessible within the loop.

Exception Handler Parameter Scope: Variables defined within a catch block are used to handle exception and they are only accessible within the catch block. On line 30, the variable “e” declared  within the catch block is only accessible between line 30 and 32.

In summary, the scope of a variable is downwards not upwards i.e. a child block can access variables defined in its parent block but not vice versa. See illustration below:

VariableScope

Keyword

Java Source FileThis is a plain text file containing Java source code and having .java extension

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s