Variable Declarations and Scope in Java
Variable Declarations: Names, Values, and Block Boundaries
Variable declarations tell Java which names your program introduces and which types those names have. When reading a short program, you need to distinguish introducing a name from changing an existing value. You also need to check where each name is available. These checks explain many errors that appear before a program can run.
Introducing a name
A declaration introduces a variable name and establishes its type. Here, seats is a local variable:
int seats = 20;The keyword int specifies the type, seats supplies the name, and 20 supplies the first value. The semicolon ends the declaration statement. An initializer is optional for an ordinary local variable, so int seats; is also a declaration.
For explicitly typed local variables, the displayed forms below show the distinction:
int seats; // declaration without an initializer
int reserved = 6; // declaration with an initializer
seats = 20; // assignment to an existing variableForm | Introduces a name? | Supplies a value? |
|---|---|---|
int seats; | Yes | No |
int seats = 20; | Yes | Yes, its first value |
seats = 20; | No | Yes, to an existing variable |
The last form assumes that seats has already been declared where the statement appears. Repeating the type would attempt another declaration rather than update the existing variable.
A declared name needs a value
A declaration without an initializer makes the name available, but it does not make a local variable ready to read. Java checks definite assignment before accepting a read of that variable.
Example: These statements are valid inside a method:
int seats;
seats = 20;
System.out.println(seats);The middle statement supplies the value before the final statement reads it. Removing that middle statement causes a compile-time error. Do not assume that an uninitialized local integer starts at zero.
Blocks set name boundaries
A variable's scope is the region of program text where its declaration makes the name available. For an ordinary local variable declared in a block, this region begins at its declaration and continues through the remainder of that block, including nested blocks.
The closing brace ends the inner variable's scope. It does not undo changes made to an outer variable. Indentation helps you see the structure, but braces establish the boundaries.
Within an ordinary method body, you cannot redeclare an enclosing local variable's name in a nested block. However, two separate blocks can each declare the same local name when their scopes do not overlap. These declarations introduce separate variables.
What to remember
- A declaration introduces a name; an assignment updates an existing variable.
- A local value must be assigned before it is read.
- Inner blocks can use outer local names, but outer code cannot use an inner local after its block ends.
- Separate blocks may reuse a local name when their scopes do not overlap.
Example: Tracking Tickets Across a Java Block
Example: A museum records reserved tickets and then adds walk-in tickets. The following code runs inside a Java method, with no other declarations using these names. A variable's scope is the region where its declaration makes its name available.
int reserved = 36;
{
int walkIns = 9;
reserved = reserved + walkIns;
System.out.println(reserved);
}
System.out.println(reserved);a) Identify the two variable declarations and explain where each name is available.
The statement int reserved = 36; declares reserved with type int and supplies its first value. Its scope continues through the rest of the enclosing method block, including the nested block and both print statements.
The statement int walkIns = 9; declares walkIns with type int and supplies its first value. Its scope continues only to the closing brace of the inner block.
The statement reserved = reserved + walkIns; has no type before the name. It updates the existing outer variable rather than declaring a new one.
The declarations are int reserved = 36; and int walkIns = 9;. Only reserved remains available after the inner block closes.
b) Find the number of tickets printed by each print statement.
Before the update, the outer variable holds ________, and the inner variable supplies ________. Both names are in scope at the update, and both have received values.
Add their values and store the result in the existing outer variable:
The first print statement therefore prints 45. The closing brace ends the scope of walkIns, but it does not undo the update to reserved. The second print statement reads the same outer variable, still holding 45.
Both print statements print 45, representing 45 tickets.
c) Suppose you append System.out.println(walkIns); after the final line. Explain the error and rewrite the fragment so this additional print is legal without changing either original printed count.
The appended statement is outside the block that declares walkIns. The name is out of scope there, even though the inner block has already executed.
Move the declaration of walkIns into the enclosing method block before the nested block. Remove its original inner declaration so that there is only one declaration of that name:
int reserved = 36;
int walkIns = 9;
{
reserved = reserved + walkIns;
System.out.println(reserved);
}
System.out.println(reserved);
System.out.println(walkIns);Now both names are in scope at the appended statement. Moving the declaration does not change the supplied values or the addition, so the original statements still print 45 and 45. The added statement prints 9, representing 9 walk-in tickets.
The repair is to declare walkIns in the enclosing block before its uses.
A bicycle rental program contains the following fragment inside a valid Java method. Assume there are no other declarations named available. Can this fragment compile successfully? Answer true or false.
int available;
{
available = 17;
}
System.out.println(available);An event program contains this fragment inside a Java method. No other variables named guests or staff exist. At each comment marker, determine whether each name is in scope. Enter exactly yes or no in every cell.
// A
int guests = 18;
// B
{
int staff = 7;
// C
}
// D| Marker | guests in scope | staff in scope |
| A | ||
| B | ||
| C | ||
| D |