Scope and Parameter Passing

In this exercise we will look at the concepts of scope and parameter passing. Scope rules define which declaration (which actual storage location) is associated with each use of a variable (or other) name.

In class we described the simple rule for potential visibility (scope) based on where the name is declared:
• in class – from { in class header to } at end of class (includes method names)
• in method – from { in method header to } at end of method
• in for – body of for (statements between {})

However, especially when there is more than one variable with the same name (e.g. as an instance variable and as a local variable), what is desired is a way to determine for each use (as opposed to declaration) of a name, to which declaration (and hence storage location) the use refers.

The text (Section 3.6) describes four rules for resolving scope (finding the declaration to which a use refers):
1. Look for a declaration of the name in the method (or constructor) in which the code resides. If one exists, this is the defining declaration. This rule applies to both formal parameter declarations and local declarations.
2. If no such declaration exists, apply step 1 looking in the immediately enclosing code unit—normally this would be a class declaration.
3. If no such declaration exists, check the public declarations of public classes from imported packages. This is how the names Turtle, forward, PI, and sin that are imported from the Media and Math packages are resolved.
4. If no such declaration exists, the name is undeclared and the reference is in error.
Consider the code in class E3 in the package Exercise_3_1 (accompanying this document). Print it for your reference. Within the code you will find 4 comments: /* Point xxx */. Trace through the execution of the code and determine the values of the variables i, j, k, l and m at each point and enter them in the first table on the answer sheet. When you are done, verify your answers by running the program and writing your answers in the second table. Note that system.out.println(…); prints text (in quotes) followed by the values of variables (separated by + in the method call) to the Interactions and Console panes. It is a quick way to see values when you are debugging a program. In this case, you might want to expand the pane using the small up arrow so you can see everything. If the results do not match what you thought, try to decide where you went wrong. The results will be discussed in the tutorial.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

/** This class demonstrates scoping and parameter passing.
** @author
** @version 2                                                      */

public class E3 {
    private int i;
    private int j;
    private int k;
    public E3 ( ) {
       int l;
       int m;
       // things are stright at Point I
       // declaration of the name is in the method
       // there is no duplicated declaration of varialbe name
       // value of global i, j, k are updated
       i = 10;
       j = 11;
       k = 12;
       l = 13;
       m = 14;
       System.out.println("Point I");    /* Point I */
       System.out.println("i = "+i);
       System.out.println("j = "+j);
       System.out.println("k = "+k);
       System.out.println("l = "+l);
       System.out.println("m = "+m);
       System.out.println("Point IV");    /* Point IV */
       System.out.println("i = "+i); // i is updated in meth()
       System.out.println("j = "+j); // j has no update for it is shaded by local variable
       System.out.println("k = "+k); // the same to k
       System.out.println("l = "+l); // scope of l is only in this function so that it cannot be changed in meth()
       System.out.println("m = "+m); // the same to m, in the call meth(m,23);
       // it passed value of m to meth() not the reference of m to meth()
       // in java, everything is object but primary type such as int, double, char ...
       // cannot passed to other function by reference, they are passed by value
    }; // constructor...
$8.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats