Tracing Scope – Building Blocks – 1Z0-829 Study Guide

Tracing Scope

The exam will attempt to trick you with various questions on scope. You’ll probably see a question that appears to be about something complex and fails to compile because one of the variables is out of scope.

Let’s try one. Don’t worry if you aren’t familiar with if statements or while loops yet. It doesn’t matter what the code does since we are talking about scope. See if you can figure out on which line each of the five local variables goes into and out of scope:

  1. public void eatMore(boolean hungry, int amountOfFood) {
  1. int roomInBelly = 5;
  1. if (hungry) {
  1. var timeToEat = true;
  1. while (amountOfFood > 0) {
  1. int amountEaten = 2;
  1. roomInBelly = roomInBelly -­amountEaten;
  1. amountOfFood = amountOfFood -­amountEaten;
  1. }
  • }
  • System.out.println(amountOfFood);
  • }

This method does compile. The first step in figuring out the scope is to identify the blocks of code. In this case, there are three blocks. You can tell this because there are three sets

of braces. Starting from the innermost set, we can see where the while loop’s block starts and ends. Repeat this process as we go on for the if statement block and method block. Table 1.10 shows the line numbers that each block starts and ends on.

TABLE 1.10Tracking scope by block
LineFirst line in blockLast line in block
while1519
if1320
Method1122

Now that we know where the blocks are, we can look at the scope of each variable. hungry and amountOfFood are method parameters, so they are available for the entire method. This means their scope is lines 11 to 22. The variable roomInBelly goes into scope on line 12 because that is where it is declared. It stays in scope for the rest of the method and goes out of scope on line 22. The variable timeToEat goes into scope on line 14 where it is declared. It goes out of scope on line 20 where the if block ends. Finally, the variable amountEaten goes into scope on line 16 where it is declared. It goes out of scope on line 19 where the while block ends.

You’ll want to practice this skill a lot! Identifying blocks and variable scope needs to be second nature for the exam. The good news is that there are lots of code examples to prac-tice on. You can look at any code example on any topic in this book and match up braces.

Applying Scope to Classes

All of that was for local variables. Luckily, the rule for instance variables is easier: they are available as soon as they are defined and last for the entire lifetime of the object itself. The rule for class, aka static, variables is even easier: they go into scope when declared like the other variable types. However, they stay in scope for the entire life of the program.

Let’s do one more example to make sure you have a handle on this. Again, try to figure out the type of the four variables and when they go into and out of scope.

  1. public class Mouse {
  • final static int MAX_LENGTH = 5;
  • int length;
  • public void grow(int inches) {
  • if (length < MAX_LENGTH) {
  • int newSize = length + inches;
  • length = newSize;
  • }
  • }
  1. }

In this class, we have one class variable, MAX_LENGTH; one instance variable, length; and two local variables, inches and newSize. The MAX_LENGTH variable is a class variable because it has the static keyword in its declaration. In this case, MAX_LENGTH goes into scope on line 2 where it is declared. It stays in scope until the program ends.

Next, length goes into scope on line 3 where it is declared. It stays in scope as long as this Mouse object exists. inches goes into scope where it is declared on line 4. It goes out of scope at the end of the method on line 9. newSize goes into scope where it is declared on line

  • Since it is defined inside the if statement block, it goes out of scope when that block ends on line