Variable Scope


Every variable that you define will have a scope associated with it. A variable's scope is the region of the program where a variable can be accessed by its simple name . Scope is different from visibility. A variable is visible when it is not shadowed (hidden) by another variable of the same name. A variable is out of scope once the block of code in which it is defined exits or the object with which it is associated is no longer referenced.

Example: Dealing with Scope

As was previously explained, a variable only has scope in the block of code in which it is declared. This can sometimes become an issue when working with operations that can throw exceptions. Such operations are usually placed in a try block. Any variables declared inside the block go out of scope when the block exits.

The ScopeDemo class declares a String variable named fileName at class scope. This variable is available to the getText() method. The getText() method reads a line of text from a file and returns the text as a String . Because I/O processes can throw exceptions, the I/O stream object creation and read operation is placed inside a try block.

 import java.io.*; public class ScopeDemo {   private String fileName = "scope.txt";   //  The getText() method reads a line of text   //  from a file.  The method has access to the   //  fileName variable declared at class scope   public String getText() {     try {       BufferedReader reader =           new BufferedReader(new FileReader(fileName));       String text = reader.readLine();     }     catch (FileNotFoundException fnfe) {        System.out.println("file not found");        return "";     }     return text;   }   public static void main(String args[]) {     ScopeDemo demo = new ScopeDemo();     System.out.println(demo.getText());   } } 

This program will not compile because the String variable named text is declared within the try block. Once the try block exits, the variable is out of scope and disappears. When we try to return text a compiler error occurs because a reference to the text variable no longer exists. One solution to this problem is to declare the variable outside of the try block and initialize it to a default value. The modified getText() method now looks like this ”

 public String getText() {   String text = "";   try {     BufferedReader reader =        new BufferedReader(new FileReader(fileName));     text = reader.readLine();    }   catch (IOException ioe) {     System.out.println("IO Exception occurred");     return text;   }   return text; } 

To run the modified program, create a text file named scope.txt and place it in the same directory as the program. If the file contained the line "This is the contents of scope.txt," the output from the program would be ”

 This is the contents of scope.txt 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net