Referencing Environment of a Subprogram
Referencing Environment of a Subprogram
1. Local Variables
○ These are variables declared within the subprogram and are only
accessible within the subprogram's scope.
○ They are created when the subprogram is called and destroyed when it
exits.
Example:
public void mySubprogram() {
int localVar = 10; // Local to mySubprogram
System.out.println(localVar);
}
○
2. Global Variables
Example:
int globalVar = 50;
○
3. Non-local Variables
Example:
public class Outer {
int outerVar = 30;
○
4. Formal Parameters
Example:
public void mySubprogram(int param) {
System.out.println(param); // Accessing formal parameter
}
○
5. Global Constants
Example:
public static final int CONSTANT = 100;
○
6. Imported Variables or Functions
Example:
import java.util.Scanner;
Output:
Global Variable: 100
Formal Parameter: 10
Local Variable: 20
Summary
1. Local Variables
2. Global Variables
3. Non-local Variables
4. Formal Parameters
5. Global Constants
6. Imported Entities
These components determine what resources are accessible during the execution of
a subprogram, and their behavior depends on the scoping rules of the language
(static or dynamic). Java primarily uses static scoping, making the referencing
environment predictable and tied to the code's structure.