Initializing Variables – Building Blocks – 1Z0-829 Study Guide

Initializing Variables

Before you can use a variable, it needs a value. Some types of variables get this value set automatically, and others require the programmer to specify it. In the following sections, we look at the differences between the defaults for local, instance, and class variables.

Creating Local Variables

A local variable is a variable defined within a constructor, method, or initializer block. For simplicity, we focus primarily on local variables within methods in this section, although the rules for the others are the same.

Final Local Variables

The final keyword can be applied to local variables and is equivalent to declaring constants in other languages. Consider this example:

  • final int y = 10;
  • int x = 20;
  • y = x + 10;  // DOES NOT COMPILE

Both variables are set, but y uses the final keyword. For this reason, line 7 triggers a compiler error since the value cannot be modified.

The final modifier can also be applied to local variable references. The following example uses an int[] array object, which you learn about in Chapter 4.

  • final int[] favoriteNumbers = new int[10];
  • favoriteNumbers[0] = 10;
  • favoriteNumbers[1] = 20;
  • favoriteNumbers = null;  // DOES NOT COMPILE

Notice that we can modify the content, or data, in the array. The compiler error isn’t until line 8, when we try to change the value of the reference favoriteNumbers.

Uninitialized Local Variables

Local variables do not have a default value and must be initialized before use. Furthermore, the compiler will report an error if you try to read an uninitialized value. For example, the following code generates a compiler error:

  • public int notValid() {
  • int y = 10;
  • int x;
  • int reply = x + y;  // DOES NOT COMPILE
  • return reply;
  • }

The y variable is initialized to 10. By contrast, x is not initialized before it is used in the expression on line 7, and the compiler generates an error. The compiler is smart enough to recognize variables that have been initialized after their declaration but before they are used. Here’s an example:

public int valid() {

int y = 10;

int x; // x is declared here

x = 3; // x is initialized here

int z; // z is declared here but never initialized or used int reply = x + y;

return reply;

}

In this example, x is declared, initialized, and used in separate lines. Also, z is declared but never used, so it is not required to be initialized.

The compiler is also smart enough to recognize initializations that are more complex. In this example, there are two branches of code:

public void findAnswer(boolean check) {

int answer;

int otherAnswer;

int onlyOneBranch;

if (check) {

onlyOneBranch = 1;

answer = 1;

} else { answer = 2;

}

System.out.println(answer);

System.out.println(onlyOneBranch); // DOES NOT COMPILE

}

The answer variable is initialized in both branches of the if statement, so the com-piler is perfectly happy. It knows that regardless of whether check is true or false, the value answer will be set to something before it is used. The otherAnswer variable is not initialized but never used, and the compiler is equally as happy. Remember, the compiler is only concerned if you try to use uninitialized local variables; it doesn’t mind the ones you never use.

The onlyOneBranch variable is initialized only if check happens to be true. The compiler knows there is the possibility for check to be false, resulting in uninitialized code, and gives a compiler error. You learn more about the if statement in Chapter 3, “Making Decisions.”

On the exam, be wary of any local variable that is declared but not initial-ized in a single line. This is a common place on the exam that could result in a “Does not compile” answer. Be sure to check to make sure it’s initial-ized before it’s used on the exam.